我使用的是 jQuery 1.7,我使用 async:false
来处理我的 AJAX 请求,但我了解到这个函数已被弃用。
我需要使用回调,但这不起作用:
$("#form").submit(function(e) {
var cnf;
$.ajax({
type: "POST",
url: 'page.php',
data: $('#form').serialize(),
async: true,
success: function(responseText) {
if(responseText.indexOf('err') != -1) {
cnf = "error";
}
else {
cnf = "success";
}
return callBack( cnf );
},
error: function() {
cnf = "error";
return callBack( cnf );
}
});
if(cnf == "success")
{
alert('ok');
}
});
HTML:
<form id="form">
<input type="text" name="email">
<input type="submit">
</form>
如果我使用 async: false
这有效。使用 callBack 我在这里看到了解决方案:wait for a jquery ajax callback from calling function
最佳答案
来自文档:
As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated
您没有将它与 jqXHR
一起使用。
也就是说,非异步 HTTP 对 JavaScript 来说是可怕的,最好避免。在回调中或从回调中完成您的工作。不要试图将数据返回给调用函数,以便它可以完成工作。
关于javascript - Ajax async false 已弃用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24187160/