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!!
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 );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With