Right now I have the following:
现在我有以下内容:
$.getJSON("firsturl", function(source1) {
$.getJSON("secondurl", function(source2) {
// I have source 1 and source 2 data here!
}
}
What is a better way of getting data from both sources.. and doing something with both of them without nesting all these $.getJSON calls?
什么是从两个源获取数据的更好方法..并且在不嵌套所有这些$ .getJSON调用的情况下对它们执行某些操作?
2 个解决方案
#1
8
You can use $.when
你可以使用$ .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 ] */
});
#2
3
Use jQuery Deferred objects with $.when
:
使用带有$ .when的jQuery Deferred对象:
var first = $.getJSON("firstUrl");
var second = $.getJSON("secondUrl");
$.when(first, second).done(function(firstResult, secondResult) {
// do stuff;
});
#1
8
You can use $.when
你可以使用$ .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 ] */
});
#2
3
Use jQuery Deferred objects with $.when
:
使用带有$ .when的jQuery Deferred对象:
var first = $.getJSON("firstUrl");
var second = $.getJSON("secondUrl");
$.when(first, second).done(function(firstResult, secondResult) {
// do stuff;
});