1、描述步骤:
1)、页面加载完毕,就执行init的方法:
$(function(){
init();
});
2)、init方法里面有两个方法,分别是ajaxA1和ajaxA2,如下所示,ajaxA2和ajaxA2是同时执行的。(两个方法里面是两个向服务端的不同接口的请求)。
function init(){
ajaxA1();
ajaxA2();
//此处ajaxA1和ajaxA2均请求完毕后执行其他的方法 other();
}
function other(){
//xxxxxxxxxx
}
2、问题:
描述中,步骤2完成后,需要判断ajaxA1和ajaxA2是否请求完毕,必须两个接口都请求完毕才执行方法other。那么请问这里怎么判断呢?
$.ajax({
type: "GET",
async:false,
url: "http://xxx/xxx1",
data: "",
success: function(msg){
//
}
});
$.ajax({
type: "POST",
async:false,
url: "http://xxx/xxx2",
data: "",
success: function(msg){
//
}
});
$.ajax({
type: "POST",
async:false,
url: "http://xxx/xxx3",
data: "",
success: function(msg){
//
}
});
async:false
让ajax不进行异步就好了,顺序执行,或者设置一个全局变量,i=0 执行完ajax i++ 判断i =2的时候执行方法
模块不可以更改的话只能用$.when(ajax1(),ajax2()).done(function(){other()}).fail(function(){console.log("error")});
$.when(ajax1(), ajax2()).then(...)