AJAX请求不能正常工作
嘿家伙和三角旗号,AJAX请求不能正常工作
我有,我想用AJAX提交表单。对于回调函数,基本上我想要发生的是,表单元素消失(.hide),并被一个div元素(这将准备好成功或某种性质)取代。然后我想让该功能暂停5秒钟,然后重定向到不同的URL。
AJAX请求成功提交(因为我收到电子邮件到指定的地址,我应该)但回调函数执行不正确。有什么建议么?
$(document).ready(function() {
var $form = $('form');
function register($form) {
$.ajax({
type: $form.attr('method'),
url: $form.attr('action'),
data: $form.serialize(),
cache : false,
dataType : 'json',
contentType: "application/json; charset=utf-8",
success: function(data){
$("#mc-embedded-subscribe-form").hide("slow");
$("#form").html('<div class="some-class">Some text that shows up upon successful AJAX request</div>')
setTimeout(5000);
$(".button").addEventListener("click", function(){
window.location.href='http://www.neuyear.net/collections/time-domination-products';
})
}
})
}
}
**
更新#1
**
这是当我最初加载网站,我收到的唯一的错误代码(我听到的只有这些错误弹出在开发工具,不影响用户?)
但是,当我提交表单时,我得到此错误代码
我也更新了我的JavaScript以下面的代码,从谁贴几个人服用的建议,但仍然没有工作100%,虽然
$(document).ready(function() {
var $form = $('form');
$form.submit(function(){
$.ajax({
type: $form.attr('method'),
url: $form.attr('action'),
data: $form.serialize(),
cache : false,
dataType : 'json',
success: function(data){
$("#mc-embedded-subscribe-form").hide("slow");
$("#form").html('<div class="some-class">Some text that shows up upon successful AJAX request</div>')
}
});
setTimeout(function(){
window.location.href='http://www.neuyear.net/collections/time-domination-products';
}, 5000)
});
});
乔伊
的setTimeout使用是错的。
参见:
$(document).ready(function() {
var $form = $('form');
function register($form) {
$.ajax({
type: $form.attr('method'),
url: $form.attr('action'),
data: $form.serialize(),
cache : false,
dataType : 'json',
contentType: "application/json; charset=utf-8",
success: function(data){
$("#mc-embedded-subscribe-form").hide("slow");
$("#form").html('<div class="some-class">Some text that shows up upon successful AJAX request</div>')
setTimeout(function(){
window.location.href='http://www.neuyear.net/collections/time-domination-products';
}, 5000);
}
})
}
});
此外,以下最好应另行申报,如果你希望用户点击一个链接,阿贾克斯成功后去。
$(".button").addEventListener("click", function(){
window.location.href='http://www.neuyear.net/collections/time-domination-products';
})
FYI,它应该等待5秒,而不是3. – Andy 2014-09-10 10:35:49
@Andy:编辑搞砸了。 – 2014-09-10 10:36:32
任何errorin您的浏览器控制台 – 2014-09-10 10:31:21
您应该通过提琴手检查HTTP响应代码,或者如果您使用的是Chrome,然后开发工具。成功基于响应代码定义。因此,对url的调用返回正确的值非常重要。 – codebased 2014-09-10 10:31:36
删除'contentType:“application/json; charset = utf-8”,':你没有发送JSON数据。 – Quentin 2014-09-10 10:32:04