最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Multiple Async AJAX Calls Best Practice - Stack Overflow

programmeradmin0浏览0评论

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: "",
    dataType: "json",
    success: function(data) { console.log(data); }
  });
  $.ajax({
    type: "GET",
    url: "",
    dataType: "json",
    success: function(data) { console.log(data); }
  });
});

Thanks for any help in advance!

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./users/ralvarenga",
    dataType: "json",
    success: function(data) { console.log(data); }
  });
  $.ajax({
    type: "GET",
    url: "https://api.github./users/dkang",
    dataType: "json",
    success: function(data) { console.log(data); }
  });
});

Thanks for any help in advance!

Share Improve this question asked Jun 30, 2014 at 18:59 richierichie 4671 gold badge7 silver badges19 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

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 );
发布评论

评论列表(0)

  1. 暂无评论