我有這個函數,使ajax調用。我在最後一段代碼註釋中描述了這個問題。Ajax jquery同步回調成功
function doop(){
var that = this;
var theold = "theold";
var thenew = "thenew";
$.ajax({
url: 'doop.php',
type: 'POST',
data: 'before=' + theold + '&after=' + thenew,
success: function(resp) {
if(resp == 1) {
$(that).siblings('.theold').html(thenew);
}
}
});
// I have some code here (out of the ajax) that **further** changes
// the .theold's html beyond what it was changed inside ajax success
// but the change depends on whether the resp (inside the success
// function) returned 1 or not, so this code out here depends on the ajax
// so it looks like I have to turn this ajax call into a sync ajax
return false;
}
基於代碼註釋中描述的問題,哪種更改最適合這種情況?
我一直在讀,設置'異步:FALSE'是壞的,一個'callback'更好。但我懷疑這很簡單。根據我更新的問題,你推薦哪一種? – Chris 2009-10-15 14:21:56
嗯,實際上你聲明的成功屬性函數是回調函數。順便說一下,我剛纔看到你將結果與一個整數進行比較,但我確定你默認獲得了文本,所以它應該是'resp ==「1」'。 – stefita 2009-10-15 14:40:02
btw。你可以在ajax請求之外的地方定義你的回調函數。可能是你看到的就是這個 - 一個叫做回調的函數。 – stefita 2009-10-15 14:42:01