我怎样才能执行,而客户端在等待服务器的响应,将运行一个功能? 这里是我的代码。 我抬头一看,发现了一个.load()函数,但是这是如何适应这个? 任何帮助将是巨大的! 谢谢
$.ajax({
type: "POST",
url: "mail.php",
data: {name: name.val(), email: email.val(), phone: phone.val(), subject: subject.val(), message: message.val()}
}).done(function(){
alert("Your message was sent. We will be in contact with you shortly.");
window.location="index.html";
});
Answer 1:
而浏览器在等待响应你调用$。阿贾克斯(后你写的)的代码非常下一行运行。
所以:
$.ajax();
yourAwesomeFN();
XhttpRequests是异步的。 无需额外的工作。
Answer 2:
你有没有在看的“beforeSend”参数 。
$.ajax({
type: "POST",
url: "mail.php",
data: {name: name.val(), email: email.val(), phone: phone.val(), subject: subject.val(), message: message.val()},
beforeSend: function(){
// Handle the beforeSend event
},
complete: function(){
// Handle the complete event
}
// ......
});
Answer 3:
而另一个被JS执行时,不能执行的功能,是其单声道线程:您可以前后虽然做的东西-在等待中显示你在寻找一种方式来建立一个消息/微调? 检查beforeSend在选项$.ajax()调用:
$.ajax({
type: "POST",
url: "mail.php",
data: {name: name.val(), email: email.val(), phone: phone.val(), subject: subject.val(), message: message.val()}
beforeSend : function(){
// do your stuff here
}
}).done(function(){
alert("Your message was sent. We will be in contact with you shortly.");
window.location="index.html";
});
文章来源: jquery execute function while ajax response is loading