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 回答