Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
169
rated 0 times [  176] [ 7]  / answers: 1 / hits: 6135  / 10 Years ago, mon, june 30, 2014, 12:00:00

I have a question regarding the Best Practice for making multiple AJAX calls on a single page.



I need to make 5 isolated calls, asynchronously. I know that $.ajax is async by nature, but I was curious if there's a cleaner or better way to do multiple AJAX calls.



An example of including multiple AJAX calls is below:



$(function() {
$.ajax({
type: GET,
url: https://api.github.com/users/ralvarenga,
dataType: json,
success: function(data) { console.log(data); }
});
$.ajax({
type: GET,
url: https://api.github.com/users/dkang,
dataType: json,
success: function(data) { console.log(data); }
});
});


Thanks for any help in advance!


More From » jquery

 Answers
3

You should use $.when().



$.when($.ajax(/page1.php), $.ajax(/page2.php)).done(function (a1, a2) {
//all AJAX requests are finished
});


Or:



$.when( $.ajax( /page1.php ), $.ajax( /page2.php ) )
.then( successFunction, failureFunction );

[#44207] Sunday, June 29, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
karyme

Total Points: 545
Total Questions: 102
Total Answers: 120

Location: French Polynesia
Member since Tue, Jul 7, 2020
4 Years ago
;