admin 管理员组文章数量: 1086019
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 badges1 Answer
Reset to default 7You 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 );
本文标签: javascriptMultiple Async AJAX Calls Best PracticeStack Overflow
版权声明:本文标题:javascript - Multiple Async AJAX Calls Best Practice - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744040607a2523184.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论