即使在调用abort之后浏览器也会等待ajax调用完成(jQuery) [英] Browser waits for ajax call to complete even after abort has been called (jQuery)

查看:309
本文介绍了即使在调用abort之后浏览器也会等待ajax调用完成(jQuery)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些(可能)长时间运行的ajax调用,如果用户导航到另一个页面,我想中止。以下jQuery代码在离开页面时调用所有挂起的XMLHttpRequest对象的中止:

I have some (potentially) long-running ajax calls that I would like to abort if the user navigates to another page. The following jQuery code calls abort on all pending XMLHttpRequest objects upon navigating away from the page:

$.ajaxSetup({
    beforeSend: function(xhr) {
        $(window).bind('beforeunload', function() {
            xhr.abort();
        });
    }
});

在测试用例中,我强制在被调用的服务器端操作上等待10秒钟。使用Firebug,我确认上面的代码确实导致所有挂起的ajax调用在我单击页面上的任何链接时立即停止。但是,浏览器仍然等待整整10秒,然后再转到下一页。 IE似乎表现出相同的行为。这是一个已知的浏览器行为吗?在这种情况下,我能做些什么让用户立即离开页面?在此先感谢。

In a test case, I force a 10-second wait on the server-side operation being called. Using Firebug, I confirmed that the above code does indeed cause all pending ajax calls to halt immediately when I click any link on the page. However, the browser still waits the full 10 seconds before moving on to the next page. IE appears to exhibit the same behavior. Is this a known browser behavior? Is there anything I can do allow the user to navigate away from the page immediately in this situation? Thanks in advance.

推荐答案

感谢您的回复!事实证明,这是一个浏览器问题我完全错了 - 问题出在服务器上。 ASP.NET序列化需要会话状态的同一会话的请求,因此在这种情况下,在那些ajax启动的请求完成之前,下一页不会在服务器上开始处理。

Thank you for your replies! It turns out I was completely wrong about this being a browser issue - the problem was on the server. ASP.NET serializes requests of the same session that require session state, so in this case, the next page didn't begin processing on the server until those ajax-initiated requests completed.

不幸的是,在这种情况下,响应ajax调用的http处理程序中需要会话状态。但是只读访问已经足够了,所以通过用IReadOnlySessionState而不是IRequiresSessionState标记处理程序,会话锁不会被保留并且问题得到解决。

Unfortunately, in this case, session state is required in the http handler that responded to the ajax calls. But read-only access is good enough, so by marking the handler with IReadOnlySessionState instead of IRequiresSessionState, session locks are not held and the problem is fixed.

希望这些信息得到证实对他人有用。

Hope this information proves useful to others.

这篇关于即使在调用abort之后浏览器也会等待ajax调用完成(jQuery)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆