ajax调用成功函数完成后执行语句

时间:2022-03-31 20:20:55

I want to execute statement after ajax success callback is completed. But inside my success callback i am writing another $.when...then syntax.

我希望在ajax成功回调完成后执行语句。但在我的成功回调中,我正在编写另一个$ .when ... then语法。

I have tried using .done(function(){ .... }); and complete function. but nothing works for me.

我尝试过使用.done(function(){....});功能齐全。但没有什么对我有用。

$.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();
}

My problem is sessionStorage.removeItem("successColors"); is getting executed at the time same when function bindUpdateValue is getting executed.

我的问题是sessionStorage.removeItem(“successColors”);正在执行函数bindUpdateValue的同时执行。

I want to execute sessionStorage.removeItem("successColors"); this line after completion of function bindUpdateValue.

我想执行sessionStorage.removeItem(“successColors”);完成函数bindUpdateValue后的这一行。

2 个解决方案

#1


How about this:

这个怎么样:

  function FirstAjaxCall(){  
     var promise = new $.Deferred();

     $.ajax({
          //other details         
        success: function (result) {//On Successful service call
            BindClipartNameDDL().then(function () { 
                // Some Code    
                MakeAnotherAjaxCall(); // This function makes another ajax call
                //Some Code
                 promise.resolve();
            });
        }
     });
     return promise;
  }

  FirstAjaxCall().then(function () { 
         sessionStorage.removeItem("successColors"); 
  });

https://*.com/a/29950249/1845408

#2


This worked for me - adding a .done callback after $.when in function bindUpdateValue

这对我有用 - 在函数bindUpdateValue中$ .when之后添加.done回调

function bindUpdateValue(data) {
    $.when(BindClipartNameDDL()).then(function () { 
    .
    .
    // Some Code    
    .
    MakeAnotherAjaxCall(); // This function makes another ajax call
    ..
    //Some Code
    }).done(function () {
        console.log("Cleaning Staerted");
        sessionStorage.removeItem("successAssignColors");
        console.log("Cleaning Done");
    });
}

#1


How about this:

这个怎么样:

  function FirstAjaxCall(){  
     var promise = new $.Deferred();

     $.ajax({
          //other details         
        success: function (result) {//On Successful service call
            BindClipartNameDDL().then(function () { 
                // Some Code    
                MakeAnotherAjaxCall(); // This function makes another ajax call
                //Some Code
                 promise.resolve();
            });
        }
     });
     return promise;
  }

  FirstAjaxCall().then(function () { 
         sessionStorage.removeItem("successColors"); 
  });

https://*.com/a/29950249/1845408

#2


This worked for me - adding a .done callback after $.when in function bindUpdateValue

这对我有用 - 在函数bindUpdateValue中$ .when之后添加.done回调

function bindUpdateValue(data) {
    $.when(BindClipartNameDDL()).then(function () { 
    .
    .
    // Some Code    
    .
    MakeAnotherAjaxCall(); // This function makes another ajax call
    ..
    //Some Code
    }).done(function () {
        console.log("Cleaning Staerted");
        sessionStorage.removeItem("successAssignColors");
        console.log("Cleaning Done");
    });
}