2012-09-26 30 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()回調,如果你願意的話。

相關問題