簡體   English   中英

如何處理兩個ajax請求?

[英]How to handle two ajax request?

你好我有這樣的順序

$(document).ready(function() {

    $("#ctl00_txtsearch").autocomplete({
        source: function(request, response) {
            $.ajax({
            // Here is the code of autocomplete which is requesting 
            // data and binding as autocomplete
            });
        });
 });
        var aa=bindonload();
    });

這是我要在頁面加載時調用的另一個函數

function bindonload() { 
    $.get( "minicart.aspx#mydatacontent", function( data ) {
        var resourceContent = data;     
        var mini=$(resourceContent).find('div#pnlminicart');
        $('#smallcart').html(mini);
    });
    return false;
}

所以,我的實際問題是首先加載頁面時

bindonload()

調用,然后自動完成(如果文本框具有某些值)? 但是,當頁面加載時,突然我開始寫到自動完成文本框中,然后直到執行bindlonload函數時,自動完成才起作用。

我不知道如何處理它,我已經使用async:true了,但是它不起作用,我不想等待第二個過程

提前致謝....

好吧..我猜..應該是..您loaddata()不應花費太多時間來加載。

如果有任何優化方法,請看一下。

如果您的ajax請求具有其他依賴關系,則無法使其並行

如果您確實打算發出並行ajax請求,則必須使用以下內容:

$.when($.ajax("URL1"), $.ajax("URL2"))
  .then(myFunc, myFailure); 

希望能幫助到你..

注意:Ajax調用不應依賴

更新:

    $.when( $.ajax( "/page1.php" ), $.ajax( "/page2.php" ) ).done(function( a1, a2 )
 { 

     // a1 and a2 are arguments resolved for the page1 and page2 ajax requests, respectively.  
    // Each argument is an array with the following structure: 
    [ data, statusText, jqXHR ]  
    var data = a1[ 0 ] + a2[ 0 ]; // a1[ 0 ] = "Whip", a2[ 0 ] = " It"  if ( /Whip It/.test( data ) ) {    alert( "We got what we came for!" );  
}}); 

在上面的示例中,您可以看到兩個ajax請求並行執行

完成兩個請求后,即成功執行兩個功能后,將執行添加操作

同樣,您可以將$.ajax("/page1.php")替換$.ajax("/page1.php") loaddata() ,然后

$.ajax("page2.php")與您的Auto Complete request

他們都將並行執行

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM