最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Aborting a jQuery getJSON XMLHttpRequest - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question edited Nov 2, 2015 at 1:47 Drew Gaynor 8,4725 gold badges41 silver badges53 bronze badges asked Aug 17, 2009 at 13:30 Gordon GuthrieGordon Guthrie 6,2642 gold badges29 silver badges52 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 2

Calling abort resets the object; the onreadystatechange event handler is removed, and readyState is changed to 0 (uninitialized).

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.

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:

First and foremost, there is no error handling for JSONP calls. If the dynamic script insertion works, you get called; if not, nothing happens. It just fails silently. For example, you are not able to catch a 404 error from the server. Nor can you cancel or restart the request.

Another great article on handling this is found here: http://www.bennadel./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!

take a look at the jquery AJAX Queue/Cache/Abort/Block Manager, maybe this can help you to prevent you current situation.

发布评论

评论列表(0)

  1. 暂无评论