让异步的多个ajax顺序执行的方法

来源:互联网 发布:淘宝达人怎么刷1万粉丝 编辑:程序博客网 时间:2024/05/08 18:32
就是说等第一个ajax传回来后再执行第二个ajax跟其他的js代码

$.ajax({
type: "POST",
url: "http://xxx/xxx.aspx",
data: "",
success: function(msg){
<!--do something-->
}
});
<!--waiting the first ajax post back-->
<!--do another ajax-->

试了几次,除了在<!--do something--> 那里 执行接下来的代码外,要顺序执行的代码放在下面的话会在第一个ajax的值还未传回来时就执行,有没有神马办法可以不在<!--do something-->那里加代码而是在下面执行呢?


1,同步:async:false
$.ajax({
type: "POST",
async:false,
url: "http://xxx/xxx.aspx",
data: "",
success: function(msg){
<!--do something-->
}
});
<!--waiting the first ajax post back-->
<!--do another ajax-->
2.通过传入一个函数解决这个问题
$.ajax({
type: "POST",
async:false,
url: "http://xxx/xxx.aspx",
data: "",
success: function(msg){
<!--do something-->
 waitreturnajax()
}
});
-----------------
function waitreturnajax()
{
<!--waiting the first ajax post back-->
<!--do another ajax-->
}


原创粉丝点击