简体   繁体   English

jQuery等到async ajax调用完成

[英]jQuery Wait until async ajax calls are finished

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. 嗨我在我的脚本中有2个ajax调用,我需要它们运行asnyc来腾出时间,但我需要第二个等到第一个完成。

$.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 谢谢

If using jQuery 1.5+, you can use jQuery.when() to accomplish this. 如果使用jQuery 1.5+,您可以使用jQuery.when()来完成此任务。 Something like (shortened the ajax calls for brevity, just pass the objects as you're doing above) 类似的东西(缩短了ajax要求简洁,只需在你上面做的时候传递对象)

$.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. 您不知道它们将以何种顺序返回,因此如果您手动滚动它,则需要检查另一个请求的状态并等待它返回。

You need to wire up the second call to be contained within the callback of your first ajax call. 您需要连接第二个调用以包含在第一个ajax调用的回调中。 Like so: 像这样:

success: function(msg)
{
    alert(msg.d.data);

    if(par02.length > 0)
    {
        // Your 2nd ajax call
    }
},

Since JavaScript doesnt run in multiple threads on the client, you can't block the thread until certain conditions are met. 由于JavaScript不会在客户端上的多个线程中运行,因此在满足某些条件之前无法阻止该线程。

Using jquery 使用jquery

$.ajax({
        type: "POST",
        async:false, // ** CHANGED **
        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");
        }
    });

Here is another answer on running dual ajax requests. 这是运行双ajax请求的另一个答案。 Like Tejs, the user makes an ajax call within the success method... 与Tejs一样,用户在成功方法中进行ajax调用...

The poster states You're better off having the success method launch a new ajax request.." 海报说你最好让成功方法发布一个新的ajax请求。“

Having two $.ajax() calls in one script 在一个脚本中有两个$ .ajax()调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM