ajax同步调用带超时
刚接触ajax,所以问一个非常基本的问题。
-- 有没有办法进行同步 ajax 调用 (async:false) 并设置超时。?
http://www.ajaxtoolbox.com/request/
超时在我的应用程序中与异步调用完美配合, 但对于一个特定的场景,我需要一个同步调用(javascript实际上应该等待,直到它从服务器返回),这也可以正常工作。但我需要处理服务器可能需要很长时间并且可能会调用 ajax 超时的情况。
还有其他关于 ajax 的标准文档我可以参考吗?
谢谢
New to ajax, so asking a very basic question.
-- Is there no way to make a Synchronous ajax call (async:false) with timeout set on it.?
http://www.ajaxtoolbox.com/request/
Timeout works perfect with Asynchronous call though in my application,
but for one particular scenario, I need a Synchronous call (the javascript should actually wait untill it hears back from the server), and this also works fine. But I need to handle a scenario where the sever could take long and a ajax timeout may be called.
Is there any other piece of standard documentation for ajax I could refer to?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
基本上,在同步ajax请求期间,浏览器被阻止,并且在浏览器被阻止时不能执行任何javascript。因此,jQuery 无法在设置的超时后中止 ajax 请求,因为 jQuery 是 javascript,而当浏览器被阻止时,javascript 无法执行。这是同步ajax 的主要缺陷。
任何时候您可能需要同步请求,都应该使用异步请求以及回调中随后发生的事情,如下所示;
Basically, during a synchronous ajax request, the browser is blocked and no javascript can be executed while the browser is blocked. Because of this, jQuery can't abort the ajax request after a set timeout because jQuery is javascript and javascript can't be executed while the browser is blocked. This is the primary flaw in synchronous ajax.
Anytime you might want a synchronous request, you should instead use an asynchronous one with what should happen afterwards in the callback, as shown below;
我不认为可以在同步调用上设置超时。当您设置“async:false”时,我相信浏览器在等待响应时实际上会锁定。仅当绝对需要时才应使用同步请求(因为浏览器锁定)。
I don't believe it's possible to set a timeout on a synchronous call. When you set "async:false", I believe the browser actually locks up while waiting for the response. You should only use a synchronous request if you absolutely need to (because of the browser locking up).