我需要进行两个不同的ajax调用,并将json数据传递给另一个函数.我怎么能使用下面的ajax调用格式呢?
$.ajax({
url:'',
type:'GET',
dataType: 'json',
success: callme
});
$.ajax({
url:'',
type:'GET',
dataType: 'json',
success: callme
});
function callme(JSON1,JSON2){
}
Run Code Online (Sandbox Code Playgroud)
这是一个完美的用例$.when(假设您正在使用jQuery):
var ajax1 = $.ajax({
url:'',
type:'GET',
dataType: 'json'
});
var ajax2 = $.ajax({
url:'',
type:'GET',
dataType: 'json'
});
$.when(ajax1, ajax2).done(function(json1, json2) {
// both requests succeeded
}).fail(function(){
// one or both requests failed
});
Run Code Online (Sandbox Code Playgroud)
在done当回调才会被调用这两个请求已成功完成; 在fail若回调将被调用的任何请求失败.