【发布时间】:2015-05-02 13:14:59
【问题描述】:
我想在ajax成功回调完成后执行语句。但在我的成功回调中,我正在编写另一个 $.when...then 语法。
我尝试过使用.done(function(){ .... }); 和complete 函数。但没有什么对我有用。
$.ajax({
type: "POST", //GET or POST or PUT or DELETE verb
url: pageURL + 'GetInitialData', // Location of the service
data: '{ "Id": "' + QueryString["ID"] + '"}',
contentType: "application/json; charset=utf-8", // content type sent to server
dataType: "json", //Expected data format from server
processdata: true, //True or False
success: function (result) {//On Successful service call
bindUpdateValue(result.d);
},
complete: function (data) {
console.log("Cleaning Staerted");
sessionStorage.removeItem("successAssignColors");
console.log("Cleaning Done");
}
});
function bindUpdateValue(data) {
$.when(BindClipartNameDDL()).then(function () {
.
.
// Some Code
.
MakeAnotherAjaxCall(); // This function makes another ajax call
..
//Some Code
});
}
function MakeAnotherAjaxCall(){
$.ajax({
type: "POST",
url: pageURL + 'PASS',
contentType: "application/json; charset=utf-8",
dataType: "json",
processdata: true,
success: function (result) {
// SOme Code
}
});
}
function BindClipartNameDDL() {
var DefferedObject = new $.Deferred();
MakeAjaxRequest('POST', pageURL + 'GetClipartNameList', '{}').done(function (result) {
var ddlClipartName = $('#ddlClipartName');
$.each(result.d, function (index, item) {
ddlClipartName.append(
$('<option/>', {
value: item.ClipartName,
text: item.ClipartName,
selected: null
})
);
});
DefferedObject.resolve();
});
return DefferedObject.promise();
}
我的问题是 sessionStorage.removeItem("successColors"); 在函数 bindUpdateValue 被执行的同时被执行。
我想在函数bindUpdateValue完成后执行sessionStorage.removeItem("successColors");这一行。
【问题讨论】:
-
什么是
BindClipartNameDDL?它与 ajax 调用有何关系?你为什么在一个承诺上使用$.when(假设BindClipartNameDDL返回一个承诺)? (因为它的工作是聚合承诺。) -
@T.J.Crowder :我已根据您的查询编辑了问题