中止jQuery getJSON XMLHttpRequest [英] Aborting a jQuery getJSON XMLHttpRequest
问题描述
原来这是一个Firebug bug的人工制品。 Ajax请求被取消,但Firebug继续将其报告为活动状态。详细信息这里。
Turns out this was an artefact of a Firebug bug. The Ajax request was cancelled, but Firebug continued reporting it as active. Details here.
我一直在运行XMLHttpRequests,我需要能够中止在某些情况下它们。
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();
当我查看Firebug时,我发现它仍在运行...
When I look in Firebug however, I see it is still running...
我在尝试中止它之前调试ajax的值。它确认它的值是和XMLHttpRequest(readyState为0)。
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).
对abh()在XMLHttpRequest上的触发方式和时间是否有限制?
Are there restrictions on how and when abort() fires on an XMLHttpRequest?
推荐答案
另一个伟大的有关处理此问题的文章,请访问: http://www.bennadel.com/blog/1500-Catching-Timeout-Errors-With-jQuery-Powered-AJAX.htm 。
Another great article on handling this is found here: http://www.bennadel.com/blog/1500-Catching-Timeout-Errors-With-jQuery-Powered-AJAX.htm.
I只是将我的.getJSON请求转换为.ajax并添加了超时/错误处理。
I simply converted my .getJSON request to .ajax and added the timeout/error handling.
谢谢!
这篇关于中止jQuery getJSON XMLHttpRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!