jQuery的等待,直到异步Ajax调用完成

编程入门 行业动态 更新时间:2024-10-27 16:37:34
本文介绍了jQuery的等待,直到异步Ajax调用完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好我有我的剧本2 Ajax调用,我需要他们asnyc运行在闲暇之余,但我还需要第二次等待,直到第一个完成。

Hi I have 2 ajax calls in my script, I need them run asnyc to spare time, but I need the second to wait until the first is finished.

$.ajax({ type: "POST", url: "getText.asmx/ws_getText", data: parO1, contentType: "application/json; charset=utf-8", dataType: "json", success: function (msg) { alert(msg.d.data); } , error: function () { chyba("chyba v požadavku", "df"); } }); if (parO2.length > 0) { $.ajax({ type: "POST", url: "getText.asmx/ws_getText", data: parO2, contentType: "application/json; charset=utf-8", dataType: "json", success: function (msg) { /*WAIT UNTIL THE FIRST CALL IS FINISHED AND THAN DO SOMETHING*/ } , error: function () { chyba("chyba v požadavku", "df"); } });

因此​​,任何想法?谢谢

So any ideas? Thanks

推荐答案

如果使用jQuery 1.5 +,你可以用 jQuery.when() 做到这一点。喜欢的东西(简称为简便起见,Ajax调用,只需把该对象为你做以上)

If using jQuery 1.5+, you can use jQuery.when() to accomplish this. Something like (shortened the ajax calls for brevity, just pass the objects as you're doing above)

$.when($.ajax("getText.asmx/ws_getText"), $.ajax("getText.asmx/ws_getText")).done(function(a1, a2){ // a1 and a2 are arguments resolved for the // first and second ajax requests, respectively var jqXHR = a1[2]; // arguments are [ "success", statusText, jqXHR ] });

您不知道以何种顺序,他们将返回,所以如果你是手工轧制这一点,你需要检查的其他请求的状态,并等待,直到它回来了。

You don't know in which order they will return so if you were rolling this by hand, you would need to check the state of the other request and wait until it has returned.

更多推荐

jQuery的等待,直到异步Ajax调用完成

本文发布于:2023-11-24 19:15:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626485.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:jQuery   Ajax

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!