1

現在、私は次のものを持っています:

$.getJSON("firsturl", function(source1) {
   $.getJSON("secondurl", function(source2) {
      // I have source 1 and source 2 data here!
   }
}

これらの $.getJSON 呼び出しをすべてネストせずに、両方のソースからデータを取得し、両方で何かを行うより良い方法は何ですか?

4

2 に答える 2

8

$.whenを使用できます

$.when($.getJSON("/firsturl"), $.getJSON("secondurl")).done(function(result1, result2){
  /* result1 and result2 are arguments resolved for the
      page1 and page2 ajax requests, respectively. 
      each argument is an array with the following 
      structure: [ data, statusText, jqXHR ] */
});
于 2013-06-24T17:08:17.090 に答える
3

jQuery Deferred オブジェクトを次のように使用します$.when

var first = $.getJSON("firstUrl");

var second = $.getJSON("secondUrl");

$.when(first, second).done(function(firstResult, secondResult) {
  // do stuff;

});
于 2013-06-24T17:08:55.177 に答える