如何确保AJAX请求调用一个特定的顺序得到的相同顺序的反应如何? [英] How to ensure AJAX requests called in a certain order get the response in the same order?

查看:165
本文介绍了如何确保AJAX请求调用一个特定的顺序得到的相同顺序的反应如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何确保AJAX请求调用一个特定的顺序得到的相同顺序的反应如何?

How to ensure AJAX requests called in a certain order get the response in the same order?

推荐答案

首先,请记住,服务器本身可能无法在收到请求的顺序返回响应。如果你第一次触发一个复杂的Ajax请求试想一下,并在第二个简单的(或许引用一些缓存的静态数据)。第二个请求将在复杂的返回之前可能回来。你可以看到这个复杂的事情;如果接收到请求的顺序是不一样的顺序的响应回来,人们如何可以使保证?

First, keep in mind that the server itself might not return responses in the order the requests are received. Imagine if you fire a complicated ajax request first, and a simple on second (perhaps referencing some cached static data). The second request will probably return before the complicated one returns. You can see how this complicates things; if the order the requests are received is not the same as the order the responses come back, how can one make assurances?

您有一些选择:

1)你可以请求同步,而不是异步的。然而,这将锁住浏览器

1) You can make the requests synchronous instead of asynchronous. However, this will lock up the browser.

2)一个更好的方法是将链条的要求,使得下一个请求仅触发后的第一个完成,只发射了第一个返回后的第二个请求。

2) A better approach would be to chain the requests such that the next request only fires after the first is completed, by only firing the second request after the first returns.

作为一个说明,如果你发现你有,你需要连锁2 Ajax操作,它可能是有意义的创建一个服务器端点来处理2经营于一体的请求。也就是说,只要一个Ajax请求你所需要的一切。

As a note, if you find that you have 2 ajax operations that you need to chain, it might make sense to create a server endpoint to handle the 2 operations in one request. i.e., just make an ajax request to do everything you need.

这篇关于如何确保AJAX请求调用一个特定的顺序得到的相同顺序的反应如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆