現在、私は次のものを持っています:
$.getJSON("firsturl", function(source1) {
$.getJSON("secondurl", function(source2) {
// I have source 1 and source 2 data here!
}
}
これらの $.getJSON 呼び出しをすべてネストせずに、両方のソースからデータを取得し、両方で何かを行うより良い方法は何ですか?
現在、私は次のものを持っています:
$.getJSON("firsturl", function(source1) {
$.getJSON("secondurl", function(source2) {
// I have source 1 and source 2 data here!
}
}
これらの $.getJSON 呼び出しをすべてネストせずに、両方のソースからデータを取得し、両方で何かを行うより良い方法は何ですか?
$.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 ] */
});
jQuery Deferred オブジェクトを次のように使用します$.when
。
var first = $.getJSON("firstUrl");
var second = $.getJSON("secondUrl");
$.when(first, second).done(function(firstResult, secondResult) {
// do stuff;
});