jquery如何使用多个ajax调用一个接一个结束
我在移动应用程序中,我使用多个 Ajax 调用从网络服务器接收数据,如下所示
function get_json() {
$(document).ready(function() {
$.ajax({
url: 'http://www.xxxxxxxxxxxxx',
data: {
name: 'xxxxxx'
},
dataType: 'jsonp',
//jsonp: 'callback',
//jsonpCallback: 'jsonpCallback',
success: function(data) {
$.each(data.posts, function(i, post) {
$.mobile.notesdb.transaction(function(t) {
t.executeSql('INSERT into bill (barcode, buildingcode, buildingaddress, flatname, flatdescription, entryseason, period, amount, pastpayments, todaypayments, paydate, receiptno) VALUES (?,?,?,?,?,?,?,?,?,?,?,?);', [post.Id, post.Code, post.Address, post.Name, post.Description, post.EntrySeason, post.Period, post.Revenue, post.PastPayments, post.todaypayments, post.paydate, post.receiptno],
//$.mobile.changePage('#page3', 'slide', false, true),
null);
});
$('#mycontent').append(post.Name);
});
}
});
$.ajax({
xxxx
});
$.ajax({
xxxx
});
});
}
我如何强制第二个 ajax 调用在第一个 ajax 调用结束后开始...第二个 ajax 调用在第二个结束后开始,依此类推?
I am in mobile app and I use multiple Ajax calls to receive data from web server like below
function get_json() {
$(document).ready(function() {
$.ajax({
url: 'http://www.xxxxxxxxxxxxx',
data: {
name: 'xxxxxx'
},
dataType: 'jsonp',
//jsonp: 'callback',
//jsonpCallback: 'jsonpCallback',
success: function(data) {
$.each(data.posts, function(i, post) {
$.mobile.notesdb.transaction(function(t) {
t.executeSql('INSERT into bill (barcode, buildingcode, buildingaddress, flatname, flatdescription, entryseason, period, amount, pastpayments, todaypayments, paydate, receiptno) VALUES (?,?,?,?,?,?,?,?,?,?,?,?);', [post.Id, post.Code, post.Address, post.Name, post.Description, post.EntrySeason, post.Period, post.Revenue, post.PastPayments, post.todaypayments, post.paydate, post.receiptno],
//$.mobile.changePage('#page3', 'slide', false, true),
null);
});
$('#mycontent').append(post.Name);
});
}
});
$.ajax({
xxxx
});
$.ajax({
xxxx
});
});
}
How can I force the 2nd ajax call to begin after the end of the first... the 3rd after the end of the 2nd and so go on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
将它们放在其所依赖的
success:
内部。Place them inside of the
success:
of the one it relies on.您已经有点接近了,但您应该将函数放在
document.ready
事件处理程序中,而不是相反。另一种方法是将 AJAX 调用放在通用函数中,然后从 AJAX 回调中调用该函数以按顺序循环一组请求:
在本例中,运行下一个 AJAX 请求的递归调用被设置为
complete
回调,以便无论当前响应的状态如何它都会运行。这意味着如果请求超时或返回 HTTP 错误(或无效响应),下一个请求仍将运行。如果您要求后续请求仅在请求成功时运行,那么使用success
回调进行递归调用可能是最好的选择。2018-08-21 更新了评论中的优点。
You are somewhat close, but you should put your function inside the
document.ready
event handler instead of the other-way-around.Another way to do this is by placing your AJAX call in a generic function and call that function from an AJAX callback to loop through a set of requests in order:
In this example, the recursive call to run the next AJAX request is being set as the
complete
callback so that it runs regardless of the status of the current response. Meaning that if the request times out or returns an HTTP error (or invalid response), the next request will still run. If you require subsequent requests to only run when a request is successful, then using thesuccess
callback to make your recursive call would likely be best.Updated 2018-08-21 in regards to good points in comments.
这是我使用了一段时间的最优雅的解决方案。它不需要外部计数器变量,并且提供了很好的封装程度。
This is the most elegant solution I've been using for a while. It doesn't require external counter variable and it provides nice degree of encapsulation.
将每个 ajax 调用包装在一个命名函数中,并将它们添加到前一个调用的成功回调中:
Wrap each ajax call in a named function and just add them to the success callbacks of the previous call:
您还可以使用 jquery when 和 then 函数。例如
https://api.jquery.com/jQuery.when/
You could also use jquery when and then functions. for example
https://api.jquery.com/jQuery.when/
我认为以下内容更加实用,因为它不会对 ajax 调用进行排序,但这肯定是一个品味问题。
现在,您可以在 ajax 请求的成功部分内迭代 window.ajax_call_count ,直到它达到发送指定数量的调用(window.ajax_calls_completed)。
I consider the following to be more pragmatic since it does not sequence the ajax calls but that is surely a matter of taste.
Now you can iterate window.ajax_call_count inside the success part of your ajax requests until it reaches the specified number of calls send (window.ajax_calls_completed).
还没有尝试过,但如果有无数的 ajax 调用,这是我能想到的最好方法。
方法1:
如果只有少数,你总是可以像这样嵌套它们。
方法2:
注意:如果是重复性任务,则使用方法1,如果要对每个数据进行不同的处理,则嵌套在方法2中 更有意义。
Haven't tried it yet but this is the best way I can think of if there umpteen number of ajax calls.
Method1:
If there are only a handful you can always nest them like this.
Method2:
Note: If it is repetitive task go for method1, And if each data is to be treated differently, nesting in method2 makes more sense.
我们可以简单地使用
这将满足您的需求。
We can simply use
This will do your need.