jQuery等待异步ajax调用完成

时间:2022-08-23 17:55:55

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调用,我需要它们运行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

所以任何想法?谢谢

4 个解决方案

#1


16  

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)

如果使用jQuery 1.5+,您可以使用jQuery.when()来完成它。类似于(缩短了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.

您不知道它们将以什么顺序返回,所以如果您手工滚动这个请求,您需要检查另一个请求的状态并等待它返回。

#2


1  

You need to wire up the second call to be contained within the callback of your first ajax call. Like so:

您需要连接第一个ajax调用的回调中包含的第二个调用。像这样:

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不会在客户机上的多个线程中运行,所以在满足某些条件之前,您不能阻塞线程。

#3


1  

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");
        }
    });

#4


0  

Here is another answer on running dual ajax requests. Like Tejs, the user makes an ajax call within the success method...

下面是运行双ajax请求的另一个答案。与Tejs一样,用户在success方法中进行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()调用

#1


16  

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)

如果使用jQuery 1.5+,您可以使用jQuery.when()来完成它。类似于(缩短了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.

您不知道它们将以什么顺序返回,所以如果您手工滚动这个请求,您需要检查另一个请求的状态并等待它返回。

#2


1  

You need to wire up the second call to be contained within the callback of your first ajax call. Like so:

您需要连接第一个ajax调用的回调中包含的第二个调用。像这样:

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不会在客户机上的多个线程中运行,所以在满足某些条件之前,您不能阻塞线程。

#3


1  

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");
        }
    });

#4


0  

Here is another answer on running dual ajax requests. Like Tejs, the user makes an ajax call within the success method...

下面是运行双ajax请求的另一个答案。与Tejs一样,用户在success方法中进行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()调用