中止 jQuery getJSON XMLHttpRequest

发布于 2024-08-02 06:21:30 字数 638 浏览 11 评论 0原文

事实证明这是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

紫南 2024-08-09 06:21:30

调用 abort 会重置对象; onreadystatechange 事件处理程序被删除,readyState 更改为 0(未初始化)。

我不认为它会取消请求本身,它只是忽略任何响应(操作事件处理程序) - 但我还没有真正尝试过。

但它绝对允许您重用该对象 - 无论浏览器是否仍然有一些线程处理旧请求。

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.

乙白 2024-08-09 06:21:30

您可以查看JSONP-Plugin
使用此插件,您可以手动中止请求。
另请参阅 IBM 技术文档中的“注意事项”图书馆:

首先,JSONP 调用没有错误处理。如果动态脚本插入有效,您就会接到电话;如果没有,则什么也不会发生。它只是默默地失败。例如,您无法从服务器捕获 404 错误。您也无法取消或重新启动请求。

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.

指尖上得阳光 2024-08-09 06:21:30

关于处理此问题的另一篇精彩文章可以在这里找到: 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!

抠脚大汉 2024-08-09 06:21:30

看看 jquery AJAX 队列/缓存/中止/块管理器,也许这可以帮助您防止当前的情况。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文