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

lbx541575387 2012-03-13 12:44:08
问:让异步的多个ajax顺序执行的方法?

就是说等第一个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-->那里加代码而是在下面执行呢?
...全文
218 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lbx541575387 2012-03-14
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 p2227 的回复:]

或者用同步,
或者设计业务的时候避免这种情况
[/Quote]

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-->
}
p2227 2012-03-13
  • 打赏
  • 举报
回复
或者用同步,
或者设计业务的时候避免这种情况
fuyun829 2012-03-13
  • 打赏
  • 举报
回复
加监听事件
Acesidonu 2012-03-13
  • 打赏
  • 举报
回复
要不同步,要不就在<!--do something-->回调里执行。
伴老思源 2012-03-13
  • 打赏
  • 举报
回复
如果非要这么设计,try


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

伴老思源 2012-03-13
  • 打赏
  • 举报
回复
[Quote=引用楼主 lbx541575387 的回复:]
试了几次,除了在<!--do something--> 那里 执行接下来的代码外,要顺序执行的代码放在下面的话会在第一个ajax的值还未传回来时就执行…………
[/Quote]

这正是异步调用的特性~

52,782

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧