所以,我知道用 jQuery 等待 ajax 请求的两种方法:
$.ajaxSetup({async: false});
此方法将使 ajax 同步。我不要那个。
我知道的第二种方法如下:
$.post("foo.html", function (foo) {
// Put all the rest of the code in here
});
我也不想这样做。
是否有第三种选择,它涉及一个处理程序,它“侦听”要完成的 ajax 请求,然后运行其余代码?
我以为可能有。
所以,我知道用 jQuery 等待 ajax 请求的两种方法:
$.ajaxSetup({async: false});
此方法将使 ajax 同步。我不要那个。
我知道的第二种方法如下:
$.post("foo.html", function (foo) {
// Put all the rest of the code in here
});
我也不想这样做。
是否有第三种选择,它涉及一个处理程序,它“侦听”要完成的 ajax 请求,然后运行其余代码?
我以为可能有。
你可以像这样在 jQuery 中使用 Deferred 对象:
$.post("foo.html").done(function() {
alert("$.post succeeded");
});
您可以在jQuery 文档中找到有关延迟对象的更多信息:延迟对象
$.ajax({
type: "POST",
url: "foo.html",
}).done(function( response ) {
// triggers when the ajax request is complete
// 'response' contains any returned values
});
如果您愿意,还有 .success() 和 .error() 回调。