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!!
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!!
Share Improve this question edited Sep 16, 2014 at 14:08 Ashish Kumar asked Jan 3, 2014 at 15:36 Ashish KumarAshish Kumar 3,0393 gold badges19 silver badges27 bronze badges 7- Is this what you're looking for? stackoverflow./questions/561046/… – elclanrs Commented Jan 3, 2014 at 15:38
- 5 Just curious, what did you answer at your interview? – Maen Commented Jan 3, 2014 at 15:39
- 1 I naswered them NO. It's not possible, but they told me - "Its very much possible" :( – Ashish Kumar Commented Jan 3, 2014 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 Commented Jan 3, 2014 at 15:56 - They didn't answer me how, that's why I am here with the same question... – Ashish Kumar Commented Jan 3, 2014 at 15:58
6 Answers
Reset to default 4If 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./jquery.when/ for more information. There's also a simple example:
$.when( $.ajax( "/page1.php" ), $.ajax( "/page2.php" ) )
.then( myFunc, myFailure );
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.
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?
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.
Call 2 requests on same event, make them async.
$("#btn").click(function(){$.get("foo.php");$.get("bar.php");});
you can do only two ajax hits with when and then, but if you wants more than two ajax hits then I would remend 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 })