7

Can I make two or multiple Ajax requests in one hit in JavaScript or jQuery?

I mean I know it seems crazy to ask this question, but earlier I have been through an interview and they asked me this question. After the interview I searched a lot on this but found nothing.

Somewhere I just found that you can put another Ajax request as the callback of first one. But this is not the real story at all.

I have a doubt, does sync or async has some role in this?

If somebody has a solution, a POC on jsfiddle or plunkr will be appreciated on the same.

JavaScript experts, please help. Thanks in Advance!!

Ashish Kumar
  • 2,991
  • 3
  • 18
  • 27
  • Is this what you're looking for? http://stackoverflow.com/questions/561046/how-many-concurrent-ajax-xmlhttprequest-requests-are-allowed-in-popular-browse – elclanrs Jan 03 '14 at 15:38
  • 5
    Just curious, what did you answer at your interview? – Maen Jan 03 '14 at 15:39
  • 1
    I naswered them NO. It's not possible, but they told me - "Its very much possible" :( – Ashish Kumar Jan 03 '14 at 15:43
  • How can you be so sure "one hit" means one usage of `$.ajax`? if the correct answer was "yes", then clearly that isn't what they meant because `$.ajax` only performs one ajax request. In that case i would have asked for clarification before answering. Knowing the right questions to ask is just as important as knowing the answers. – Kevin B Jan 03 '14 at 15:56
  • They didn't answer me how, that's why I am here with the same question... – Ashish Kumar Jan 03 '14 at 15:58
  • you think they might ment to ajax call being fired in the Success method of the first ajax call? – Ori Refael Jan 03 '14 at 16:13
  • means one XMLHttpRequest and send multiple urls at once. – Ashish Kumar Jan 04 '14 at 16:51

6 Answers6

4

If you are using jQuery you can make use of the deferred objects. Basically you can perform multiple ajax requests, and when all are done, one callback is executed.

Have a look at http://api.jquery.com/jquery.when/ for more information. There's also a simple example:

$.when( $.ajax( "/page1.php" ), $.ajax( "/page2.php" ) )   
    .then( myFunc, myFailure );
T. Junghans
  • 11,385
  • 7
  • 52
  • 75
2

In short -- no, you cannot.

You can make multiple callbacks to do them all in order, or you can make all the requests to the multiple points on the server side.

You can always do two ajax requests in a row, but there is no guarantee in what order they will return to their respective callbacks.

qwertynl
  • 3,912
  • 1
  • 21
  • 43
  • I answered them as NO, but they told me its possible. I am wondering, how? – Ashish Kumar Jan 03 '14 at 15:41
  • 1
    Well it isn't... Or it all depends on what they _mean_ by "in one hit" – qwertynl Jan 03 '14 at 15:41
  • 1
    In one button click, of course it's possible. It all depends on what they mean by "hit". "hit" could mean a button click, one trip to the server, a keypress, or multiple requests simultaneously. button click would be yes, one trip would be no, keypress would be yes, multiple request simultaneously would be yes. so the safest answer would be "yes" in my opinion. – Kevin B Jan 03 '14 at 15:51
  • He is asking about **one** http request I believe @KevinB see the OPs other comments. – qwertynl Jan 03 '14 at 15:52
  • @qwertynl No, i don't think he knows what he means by "hit" because what he is referring to is what was asked by his interviewer. We aren't talking to his interviewer, therefore we can't positively know what they were referring to in this case. "hit" isn't a technical term that we can attach to a javascript interaction without further context. – Kevin B Jan 03 '14 at 15:53
  • See his comment [here](http://stackoverflow.com/questions/20907226/can-i-make-two-ajax-request-in-one-hits-in-javascript-or-jquery/20907261?noredirect=1#comment31384013_20907383) @KevinB – qwertynl Jan 03 '14 at 15:55
  • I see his comment, i disagree with it. – Kevin B Jan 03 '14 at 15:56
  • @AshishKumar `Promises`. Google them. **Not** jQuery's "promises". Think `when.js`. – Jimbo Jan 03 '14 at 16:04
1

Look, if you want to send 2 urls on single xmlHttpRequest, I think, this is not possible.

And suppose, it were possible, how we would be able to find the data send by server as response that which response is for which url request?

Ravi
  • 95
  • 9
0

Call 2 requests on same event, make them async.

$("#btn").click(function(){$.get("foo.php");$.get("bar.php");}); 
rene
  • 41,474
  • 78
  • 114
  • 152
zozo
  • 8,230
  • 19
  • 79
  • 134
0

I'm not sure what you mean by hit? but yes put plainly.

async - doesn't wait for the response from the ajax request

sync - waits until a response from the ajax

depends on what you want to do.

EDIT: after reading other responses. to clarify, if you want multiple concurrent simultaneous ajax requests you need a concurrent connection for each. that is how it possible.

Aage Torleif
  • 1,907
  • 1
  • 20
  • 37
0

you can do only two ajax hits with when and then, but if you wants more than two ajax hits then I would recommend ajax always.

    $.ajax({
           url: '/Job/multipleajax/',
           contentType: "application/json; charset=utf-8",
           dataType: "json",
           type: "POST",
           data: JSON.stringify(id),
           success: function (response) {
    alert("first ajax hit");
    }
    }).always(function () {
    // second ajax call here
    }).always(function () {
    // Third ajax call code  here
    })
Mukesh Salaria
  • 3,345
  • 1
  • 16
  • 21