中止 jQuery getJSON XMLHttpRequest
事实证明这是 Firebug bug 的产物。 Ajax 请求被取消,但 Firebug 继续将其报告为活动状态。详细信息 此处。
我长时间运行 XMLHttpRequest,并且需要能够在某些情况下中止它们。
我以正常方式设置请求:
ajax = $.getJSON(url + "?updates", function(data) { ...; });
稍后我想中止此请求,看起来很简单:
ajax.abort();
但是,当我查看 Firebug 时,我看到它仍在运行...
我在之前 console.log 出了 ajax 的值尝试并中止它,它确认其值是 XMLHttpRequest(readyState 为 0)。
对 XMLHttpRequest 触发 abort() 的方式和时间是否有限制?
Turns out this was an artefact of a Firebug bug. The Ajax request was cancelled, but Firebug continued reporting it as active. Details here.
I have long running XMLHttpRequests and I need to be able to abort them under certain circumstances.
I set up the request in a normal way:
ajax = $.getJSON(url + "?updates", function(data) { ...; });
Later on I want to abort this request, seems straightforward:
ajax.abort();
When I look in Firebug however, I see it is still running...
I console.log out the value of ajax just before I try and abort it, it confirms that its value is and XMLHttpRequest (with a readyState of 0).
Are there restrictions on how and when abort() fires on an XMLHttpRequest?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不认为它会取消请求本身,它只是忽略任何响应(操作事件处理程序) - 但我还没有真正尝试过。
但它绝对允许您重用该对象 - 无论浏览器是否仍然有一些线程处理旧请求。
i do not think it cancels the request itself, it just ignores any response (manipulating the event handler)- but i didnt really try it out yet.
but it will deifnitely allow you to reuse the object - no matter if the browser still has some thread working with the old request.
您可以查看JSONP-Plugin
使用此插件,您可以手动中止请求。
另请参阅 IBM 技术文档中的“注意事项”图书馆:
you may take a look at the JSONP-Plugin
With this Plugin you have the ability to manually abort the request.
Also take a look at "a cautionary note" at IBM's Technical library:
关于处理此问题的另一篇精彩文章可以在这里找到: http://www.bennadel.com/blog/1500-Catching-Timeout-Errors-With-jQuery-Powered-AJAX.htm。
我只是将 .getJSON 请求转换为 .ajax 并添加了超时/错误处理。
谢谢!
Another great article on handling this is found here: http://www.bennadel.com/blog/1500-Catching-Timeout-Errors-With-jQuery-Powered-AJAX.htm.
I simply converted my .getJSON request to .ajax and added the timeout/error handling.
Thanks!
看看 jquery AJAX 队列/缓存/中止/块管理器,也许这可以帮助您防止当前的情况。
take a look at the jquery AJAX Queue/Cache/Abort/Block Manager, maybe this can help you to prevent you current situation.