简体   繁体   English

jQuery-使用WHEN和DONE序列化ajax调用

[英]jquery - serializing ajax calls with WHEN and DONE

I have few ajax methods and I want to execute some code after successful completion of all these ajax calls. 我几乎没有ajax方法,我想在成功完成所有这些ajax调用后执行一些代码。 I can't alter or redefine the ajax methods. 我无法更改或重新定义Ajax方法。 Please let me know , how to achieve this. 请让我知道,该如何实现。

I tried with WHEN but it get called immediately and not waiting for all calls to be completed.( As suggested , once I added return in loadData1() , it works fine. ) 我尝试使用WHEN,但是它立即被调用,而不是等待所有调用完成。( 如所建议的,一旦在loadData1()中添加return,它就可以正常工作。

Now my problem is , if any of the request(loadData1() or loadData2()) is having error then "then()" is not getting executed . 现在我的问题是,如果任何request(loadData1()或loadData2())出错,那么“ then()”就不会执行。 Please let me know , how to achieve this. 请让我知道,该如何实现。

var load1 = loadData1();
var load2 = loadData2();
var load3 = loadData3();
var load4 = loadData4();

$.when(load1, load2, load3,load4).then(function () {
        console.log("All done");
});

function loadData1() {
    return $.getJSON("http://10.1.2.3/cgi-bin/GetData1.cgi", function (data) {
        console.log(data);
    });
}

Thanks 谢谢

You can use function like 您可以使用类似的功能

 function first() {
   return $.ajax(...);
}

function second(data, textStatus, jqXHR) {
   return $.ajax(...);
}

function third(data, textStatus, jqXHR) {
   return $.ajax(...);
}

function main() {
    first().then(second).then(third);
}

check this for more detail jquery-promises 检查更多细节jquery-promises

Try this 尝试这个

var promise = loadData1();


var load2 = loadData2();
var load3 = loadData3();
var load4 = loadData4();

 promise.then(loadData2).then(loadData3).then(loadData4).then(function (E) {
                       //
                    });

function loadData1() {
    $.getJSON("http://10.1.2.3/cgi-bin/GetData1.cgi", function (data) {
        console.log(data);
    });

var deferred = new $.Deferred();
    deferred.resolve();
    return deferred.promise();
}

You should return something in your functions. 您应该在函数中返回一些内容。 This is the first thing. 这是第一件事。 The second when you are waiting you must wait for results of functions so instead of load1, load2, load3, load4 use load1(), load2(), load3(), load4() . 第二个当您等待时,您必须等待函数的结果,因此load1, load2, load3, load4使用load1(), load2(), load3(), load4()来代替load1, load2, load3, load4

Below an example simulating 4 Deferred functions (your ajax requests). 下面的示例模拟了4个Deferred函数(您的Ajax请求)。

 // load1, load2, load3, load4 are defined to simulate promises and some work behind it // here is var load1 = loadData1(); var load1 = function () { return $.Deferred(function(defer) { setTimeout(function() { $('#log').append('<div>loadData1()</div>'); defer.resolve(); }, 150); }); } // here is var load2 = loadData2(); var load2 = function () { return $.Deferred(function(defer) { setTimeout(function() { $('#log').append('<div>loadData2()</div>'); defer.resolve(); }, 600); }); } // here is var load3 = loadData3(); var load3 = function () { return $.Deferred(function(defer) { setTimeout(function() { $('#log').append('<div>loadData3()</div>'); defer.resolve(); }, 300); }); } // here is var load4 = loadData4(); var load4 = function () { return $.Deferred(function(defer) { setTimeout(function() { $('#log').append('<div>loadData4()</div>'); defer.resolve(); }, 200); }); } $(document).ready(function() { $.when(load1(), load2(), load3(), load4()) .then(function () { $('#log').append('<div>All done.</div>'); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="log"></div> 

You can try to use a utility function like 您可以尝试使用类似

function allCompleted(array) {
  var deferred = jQuery.Deferred(),
    counter = array.length,
    results = [];
  $.each(array, function(i, item) {
    item.always(function() {
      results[i] = [].slice.call(arguments, 0)
      if (--counter == 0) {
        deferred.resolveWith(undefined, results);
      }
    });
  });
  return deferred.promise();
}

then 然后

var load1 = loadData1();
var load2 = loadData2();
var load3 = loadData3();
var load4 = loadData4();

allCompleted([load1, load2, load3,load4]).then(function () {
        console.log("All done");
});

Demo: Fiddle 演示: 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM