【问题标题】:execute statement after ajax call success function completesajax调用成功函数完成后执行语句
【发布时间】: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 :我已根据您的查询编辑了问题

标签: jquery ajax


【解决方案1】:

这个怎么样:

  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://stackoverflow.com/a/29950249/1845408

【讨论】:

  • 我在函数MakeAnotherAjaxCall 中添加了return 关键字。但它不起作用。请检查问题编辑
  • @Shaggy 你试过把这个sessionStorage.removeItem("successAssignColors"); 放在function bindUpdateValue(data) {} 的末尾
【解决方案2】:

这对我有用 - 在函数 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");
    });
}

【讨论】:

    猜你喜欢
    • 2020-01-31
    • 2014-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-21
    • 1970-01-01
    • 2015-06-13
    • 1970-01-01
    相关资源
    最近更新 更多