浏览器等待ajax调用完成,即使调用了abort (jQuery)

时间:2021-11-30 08:00:52

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:

我有一些(潜在的)长时间运行的ajax调用,如果用户导航到另一个页面,我将中止它。下面的jQuery代码调用所有挂起的XMLHttpRequest对象上的abort:

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

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.

在一个测试用例中,我在被调用的服务器端操作上强制等待10秒。使用Firebug,我确认了上面的代码确实会导致当我单击页面上的任何链接时,所有挂起的ajax调用立即停止。然而,浏览器仍然要等上整整10秒,然后才能进入下一个页面。IE也表现出同样的行为。这是一个已知的浏览器行为吗?在这种情况下,我能做什么让用户立即离开页面?提前谢谢。

6 个解决方案

#1


27  

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.

谢谢你的回复!事实证明,我完全错误地认为这是一个浏览器问题——问题出在服务器上。ASP。NET序列化需要会话状态的同一会话的请求,因此在本例中,下一页直到ajax发起的请求完成后才在服务器上开始处理。

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.

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

Hope this information proves useful to others.

希望这些信息对其他人有用。

#2


10  

Regarding Todd's own answer to this question...

关于托德自己对这个问题的回答……

I just had this issue with PHP and the same solution would have worked. However I needed the information in the session. For PHP developers you can call session_write_close() to close and write out your session in the middle of the request. This will free up the session for the other requests.

我刚刚在PHP中遇到了这个问题,同样的解决方案也可以。但是我需要在会议中提供的信息。对于PHP开发人员,可以调用session_write_close()来关闭并在请求中间写入会话。这将为其他请求释放会话。

#3


3  

You might want to check a weird side effect of abort()

您可能需要检查abort()的一个奇怪的副作用

When the abort() method is used, the readystatechange event fires in Explorer and Mozilla. Worse, readyState = 4, which means that the average xmlhttp script assumes the data has been loaded correctly. This can give very weird effects.

当使用abort()方法时,将在Explorer和Mozilla中触发readystatechange事件。更糟糕的是,readyState = 4,这意味着一般的xmlhttp脚本假定数据已被正确加载。这会产生非常奇怪的效果。

documented here:

记录:

http://www.quirksmode.org/blog/archives/2005/09/xmlhttp_notes_a_1.html

http://www.quirksmode.org/blog/archives/2005/09/xmlhttp_notes_a_1.html

#4


2  

Are you sure that you are using an asychronous request? If the browser blocks during the entire request, you are using a synchronous request (async parameter is false)

你确定你正在使用一个异步请求吗?如果浏览器在整个请求期间阻塞,则使用同步请求(async参数为false)

#5


1  

The server issue is also the case with the Apache/Php server that I am working with. Removed the session_start on a script that did not need it (AJAX) and everything is working as expected. Thanks to Todd who pointed out a like problem!

服务器问题也是我正在处理的Apache/Php服务器的问题。删除了不需要的脚本(AJAX)上的session_start,一切都按预期运行。感谢托德指出了一个类似的问题!

#6


0  

Reference to the accepted answer:

参考已接受的答案:

http://improve.dk/optimizing-performance-programmatically-setting-readonlysessionstate/

http://improve.dk/optimizing-performance-programmatically-setting-readonlysessionstate/

About IRequiresSessionState

关于IRequiresSessionState

What this means is that, for a given session, only one request can execute concurrently. Any other requests, from that same session, will block, waiting for the session to be released

这意味着,对于给定的会话,只有一个请求可以并发执行。来自同一会话的任何其他请求都将被阻塞,等待会话被释放

#1


27  

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.

谢谢你的回复!事实证明,我完全错误地认为这是一个浏览器问题——问题出在服务器上。ASP。NET序列化需要会话状态的同一会话的请求,因此在本例中,下一页直到ajax发起的请求完成后才在服务器上开始处理。

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.

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

Hope this information proves useful to others.

希望这些信息对其他人有用。

#2


10  

Regarding Todd's own answer to this question...

关于托德自己对这个问题的回答……

I just had this issue with PHP and the same solution would have worked. However I needed the information in the session. For PHP developers you can call session_write_close() to close and write out your session in the middle of the request. This will free up the session for the other requests.

我刚刚在PHP中遇到了这个问题,同样的解决方案也可以。但是我需要在会议中提供的信息。对于PHP开发人员,可以调用session_write_close()来关闭并在请求中间写入会话。这将为其他请求释放会话。

#3


3  

You might want to check a weird side effect of abort()

您可能需要检查abort()的一个奇怪的副作用

When the abort() method is used, the readystatechange event fires in Explorer and Mozilla. Worse, readyState = 4, which means that the average xmlhttp script assumes the data has been loaded correctly. This can give very weird effects.

当使用abort()方法时,将在Explorer和Mozilla中触发readystatechange事件。更糟糕的是,readyState = 4,这意味着一般的xmlhttp脚本假定数据已被正确加载。这会产生非常奇怪的效果。

documented here:

记录:

http://www.quirksmode.org/blog/archives/2005/09/xmlhttp_notes_a_1.html

http://www.quirksmode.org/blog/archives/2005/09/xmlhttp_notes_a_1.html

#4


2  

Are you sure that you are using an asychronous request? If the browser blocks during the entire request, you are using a synchronous request (async parameter is false)

你确定你正在使用一个异步请求吗?如果浏览器在整个请求期间阻塞,则使用同步请求(async参数为false)

#5


1  

The server issue is also the case with the Apache/Php server that I am working with. Removed the session_start on a script that did not need it (AJAX) and everything is working as expected. Thanks to Todd who pointed out a like problem!

服务器问题也是我正在处理的Apache/Php服务器的问题。删除了不需要的脚本(AJAX)上的session_start,一切都按预期运行。感谢托德指出了一个类似的问题!

#6


0  

Reference to the accepted answer:

参考已接受的答案:

http://improve.dk/optimizing-performance-programmatically-setting-readonlysessionstate/

http://improve.dk/optimizing-performance-programmatically-setting-readonlysessionstate/

About IRequiresSessionState

关于IRequiresSessionState

What this means is that, for a given session, only one request can execute concurrently. Any other requests, from that same session, will block, waiting for the session to be released

这意味着,对于给定的会话,只有一个请求可以并发执行。来自同一会话的任何其他请求都将被阻塞,等待会话被释放