简体   繁体   English

如何处理两个ajax请求?

[英]How to handle two ajax request?

Hello i have sequence like this 你好我有这样的顺序

$(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();
    });

Here is the another function which i want to call on page load 这是我要在页面加载时调用的另一个函数

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

So , my actual problem is when page is loaded first of all 所以,我的实际问题是首先加载页面时

bindonload() bindonload()

called and then autocomplete if textbox have some values right? 调用,然后自动完成(如果文本框具有某些值)? But when page is loaded and suddenly i started to write into autocomplete textbox then untill bindlonload function gets executed autocomplete will not work. 但是,当页面加载时,突然我开始写到自动完成文本框中,然后直到执行bindlonload函数时,自动完成才起作用。

I don't have idea how to handle it i have used async:true but its not working i don't want to wait for second process 我不知道如何处理它,我已经使用async:true了,但是它不起作用,我不想等待第二个过程

Thanks in advance.... 提前致谢....

Well..what i guess..should be..you loaddata() should not be taking too much of time to load. 好吧..我猜..应该是..您loaddata()不应花费太多时间来加载。

If there is any way to optimize , look of that. 如果有任何优化方法,请看一下。

If you ajax request has a dependency on the other, then you can not make it parallel 如果您的ajax请求具有其他依赖关系,则无法使其并行

If you really intend to make parallel ajax requests, you have to make use of the following: 如果您确实打算发出并行ajax请求,则必须使用以下内容:

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

Hope it helps.. 希望能帮助到你..

Note : The Ajax calls should not be dependent 注意:Ajax调用不应依赖

Updated: 更新:

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

In the above example , you can see two ajax requests executes parallel 在上面的示例中,您可以看到两个ajax请求并行执行

After the two requests are done, ie on sucess of two functions , the add operation is being performed 完成两个请求后,即成功执行两个功能后,将执行添加操作

Similarly , you can replace $.ajax("/page1.php") with your loaddata() and then 同样,您可以将$.ajax("/page1.php")替换$.ajax("/page1.php") loaddata() ,然后

$.ajax("page2.php") with your Auto Complete request . $.ajax("page2.php")与您的Auto Complete request

Both of them will execute Parallely 他们都将并行执行

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

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