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