繁体   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