2012-09-26 27 views
1

所以,我知道的两种方法来等待与jQuery Ajax请求:如何在ajax完成后继续执行脚本而不使脚本同步或嵌入内容?

$.ajaxSetup({async: false}); 

这种方法将使得Ajax同步。我不要那个。

,我知道的第二种方法如下:

$.post("foo.html", function (foo) { 
// Put all the rest of the code in here 
}); 

我不想做,要么。

是否有第三个选项,它涉及一个处理程序,它监听ajax请求是否完成,然后运行其余的代码?

我以为可能会有。

+0

为什么你不想使用$ .post?这只是$ .ajax的简写,这是我的建议。 – Sara

+0

不,我很可能误解了我的问题。我想要一个侦听器来监听'$ .post'函数的完成情况。如Sara所说, – think123

+0

,使用$ .post有什么问题?它完全符合你的要求。您可以创建一个方法,比如runAfterAjax(),并将其粘贴到$ .post()的回调函数中 – kennypu

回答

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()回调,如果你愿意的话。

相关问题