[英]AJAX function calling on success
我的javascript代碼 -
function updateWhatIfPrivacyLevelRemove(recordId, correspondingDetailIDs) {
var ajaxCall = $.ajax({ data: { Svc: cntnrRoot,
Cmd: 'updateWhatIfPrivacyLevel',
updatePrivacyAction: 'Remove',
recordID: recordID
},
dataType: "text",
context: this,
cache: false
});
$.when(ajaxCall).then(updateWhatIfPrivacyLevelRemoveSuccess(recordID, correspondingResidentDetailIDs));
}
function updateWhatIfPrivacyLevelRemoveSuccess(recordID, correspondingResidentDetailIDs) {
//several other lines of non-related code
$.ajax({ data: { Svc: cntnrRoot,
Cmd: 'handleResidentRow',
recordID: 1,
active: 0
},
dataType: "text",
context: this,
cache: false
});
}
在我的C#代碼中,我處理'updateWhatIfPrivacyLevel'和'handleResidentRow'的回調。 我可以告訴在updateWhatIfPrivacyLevel之前調用handleResidnetRow的AJAX回調。
為什么?
當您嘗試設置回調時,您實際上正在調用該函數。 換句話說,你沒有將“updateWhatIf ...”函數作為回調傳遞,你傳遞的是它的返回值(看起來總是undefined
)。
試試這個:
$.when(ajaxCall).then(function() {
updateWhatIfPrivacyLevelRemoveSuccess(recordID, correspondingResidentDetailIDs);
});
對函數名的引用是對函數的引用,可用於將函數作為回調傳遞。 但是,對函數后跟( )
的引用是對函數的調用 ,將對其進行求值,以便可以在周圍表達式的上下文中使用返回值。 因此,在你的代碼中,你將undefined
(函數調用的結果)傳遞給.then()
方法,當然這不會做你想要的。
重要的是要記住,jQuery只是JavaScript,特別是JavaScript函數庫。 雖然.then()
看起來像一個語言結構,但它不是 - JavaScript解釋器不會以任何方式特別對待它。
在我的建議中使用匿名函數的替代方法是在較新的瀏覽器中對Function原型使用.bind()
方法。 這基本上對你來說是一樣的,但它在風格上更像傳統的函數式編程。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.