0

Possible Duplicate:
Parallel asynchronous Ajax requests using jQuery

I need to create 2 ajax requests one after the other. And then display the result of the one that has finished first. The problem is that second one always waits for first one to finish. I tried to abort first one but still the second is waiting for first one to finish. Is there any workaround?

Basically first one calls php script that parses some XML file and inserts records into a MySQL table. Second one calls PHP script which needs to return 20 records that first one has imported.

I tried with two ajax calls and the first one called from iframe. But result is always same. Second one is waiting for the first one to finish. Only way around that I find is to create subdomain and call first one with subdomain. But this not working in IE.

Can anybody explain how solve this?

Maybe I need to describe my problem more. First I call ajax that execution time is cca 2 min and while first one is in execution I need to have multiple requests that will retrive records that are imported by first one.

Community
  • 1
  • 1
jere_hr
  • 284
  • 3
  • 11

2 Answers2

3

You can try to do by using AJAX callback. You can basically enter a nested callback and get the job done. The 1st callback will be invoked when you get the parsed XML and the second one will be called by the 1st callback.

Update for a code example:

var callback2 = function(data)
{
   // Callbacks complete
   // Now do some more stuff
}
var callback1 = function(data)
{
   //do something;
   $.ajax({
    url: '/echo/html/',
    success: function(data) {
        callback2(data);
    }
});   
}
$.ajax({
    url: '/echo/html/',
    success: function(data) {
        callback1(data);
    }
});

In the example above callback2 is invoked when a second Ajax call is complete.

redDragonzz
  • 1,543
  • 2
  • 15
  • 33
  • Can you give me code example how to get response of second one before first one is finished? I used $.ajax but if I can somehow manage it with this that would be great. Thank you for fast response. – jere_hr Oct 23 '12 at 08:46
  • oh wait for parallel requests you can invoke the Ajax request in sequence and then invoke the success callback. The one that gets finished first would call the callback – redDragonzz Oct 23 '12 at 08:58
  • If I understand this correctly. Callback1 will be called after first one is finished. Then will trigger callback2. Maybe I need to describe my problem more. First I call ajax that execution time is cca 2 min and while first one is in execution I need to have multiple requests that will retrive records that are imported by first one. – jere_hr Oct 23 '12 at 09:01
  • callback1 will recv some data right? That data can be asynchronous processed by the second ajax call. – redDragonzz Oct 23 '12 at 09:04
  • I try that thing with $([1,2]).each(function(){...ajax call... but that didnt help. It looks like I can't have 2 separate request at same time?! – jere_hr Oct 23 '12 at 09:07
  • no I don't care about response of first one. I just want to trigger it and forget it – jere_hr Oct 23 '12 at 09:09
  • If you want to execute it and forget it check http://api.jquery.com/jQuery.when/ that might help u run ur method at the right time (possibly when u have data) – redDragonzz Oct 23 '12 at 13:02
-1

Call them sequentially. After you call the first one, it'll be executing in asynchronous manner. Then you call the second one. At that time first request will be still running. But its not guarranteed that second request will be sent just after first one. So you can have some timeout. See the example bellow.

$.post('ajax/first_call', function(data) {
    // process returned data of first ajax call
});
window.setTimeout(function(){
    $.post('ajax/second_call', function(data) {
        // process returned data of second ajax call
    });
}, 2000);

Here the second ajax call is requested after 2 second. And it does not depend on first call.

Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
  • But I don't want to wait first one to finish to get response of second one. I need to have response of second one before first one is finished. – jere_hr Oct 23 '12 at 08:54
  • You said `Basically first one calls php script that parses some XML file and inserts records into a MySQL table. Second one calls PHP script which needs to return 20 records that first one has imported.` Here the first script should have ended before you call the second one. Otherwise how do the second call get the 20 records that first one imported? – Shiplu Mokaddim Oct 23 '12 at 08:58
  • Maybe I need to describe my problem more. First I call ajax that execution time is cca 2 min and while first one is in execution I need to have multiple requests that will retrive records that are imported by first one. – jere_hr Oct 23 '12 at 09:05
  • Then send request one after another. While requesting the second one, First one will be executing. @jere_hr see the update – Shiplu Mokaddim Oct 23 '12 at 11:08
  • I tried that but I can't get response from second one till first one not done. That exactly is my issue. – jere_hr Oct 23 '12 at 11:14
  • Do you know that you are talking about two mutually exclusive events? I am tired explaining you. No one can solve your issue for sure. Its something like, You want to have your breakfast. But in the half way you want your belly will be filled. – Shiplu Mokaddim Oct 23 '12 at 12:27
  • I didn't know is there any solution for this or not. But I want to thank you and all other guys that tried to help me. I was hoping that async ajax can be similar to thread, independent of other async calls in same time. – jere_hr Oct 23 '12 at 12:49
  • Its independent. But I think you dont know how it works. And you are asking something unrealistic. – Shiplu Mokaddim Oct 23 '12 at 12:51
  • I was confused because when I do 2 async requests at same time or one after other on two different domains then all working as I want in FF i Chrome but not in IE. Other one don't need to wait first to finish. I won't bother you more. – jere_hr Oct 23 '12 at 12:56
  • You *must* send ajax to same domain!! – Shiplu Mokaddim Oct 23 '12 at 12:58