1

I have two consecutive Async Ajax calls as below, when I execute that, I am expecting '1st complete' will be print if it takes less time than call2 (id=2) vice-versa '2nd complete' will be print if it takes less time than call1 (id=1). But, I observe that second call always wait for first call to complete. get confuse with Async. please help.

    $(function(){
       jQuery.ajax({ 
            url: 'Home/getItem', 
            data : { id : 1 }, 
            async:true,
            success: function(){ console.log('1st complete'); }
        }),
        jQuery.ajax({ 
            url: 'Home/getItem', 
            data : { id : 2 }, 
            async:true,
            success: function(){ console.log('2nd complete'); }
        })
});

When I check same functions with core php, it return expected results.

iTechOwl
  • 150
  • 11
  • @Alex you seem to have misunderstanding of `async:true` – charlietfl Oct 26 '15 at 12:02
  • maybe this has something to do with it: http://stackoverflow.com/questions/9189591/multiple-ajax-requests-for-same-url – Alex Oct 26 '15 at 12:06
  • also worth reading: http://stackoverflow.com/questions/1060539/parallel-asynchronous-ajax-requests-using-jquery – Alex Oct 26 '15 at 12:08
  • @alex, no both request start at same time but response get in order.. – iTechOwl Oct 26 '15 at 12:11
  • which technology you are using at backend? is it a .Net? – vijayP Oct 26 '15 at 12:12
  • @bhups but what is your expected behavior? and how are you sure it "waits" until the first call is done? which browser are you using? – Alex Oct 26 '15 at 12:12
  • @Alex, I confirmed with results when execute single call at a time, call1 takes nearly 3sec and call2 takes nearly 600ms. as call2 take less time I want call2 will be response first and then call1. and I am using chrome. – iTechOwl Oct 26 '15 at 12:16

1 Answers1

2

You are seeing such output because; almost in each server side technology there is a session management. Now assume that your getItem function/method do some job and update the session object. So by default server always prevent concurrent call to same server side method by same user. If first request is going on then second request gets pushed onto the queue. And once first request gets completed; the pending request gets executed. By this way session state modified by first request will get available to 2nd request and so on.

In .Net technology there are annotation to make controller/methods session unaware so that concurrent ajax calls can be made. Via these annotations we declare that the related controller/method doesn't deal with session state and hence OK to call concurrently.

You can read more about it in below SO question:

Why would multiple simultaneous AJAX calls to the same ASP.NET MVC action cause the browser to block?

Similarly there has to be something in PHP. Try searching it and you will get the clue.

Community
  • 1
  • 1
vijayP
  • 11,432
  • 5
  • 25
  • 40