您的位置:首页 > 其它

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

2012-03-14 12:22 253 查看
就是说等第一个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-->

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: