可以使用卸载事件来可靠地触发ajax请求吗? [英] Can the unload Event be Used to Reliably fire ajax Request?

查看:123
本文介绍了可以使用卸载事件来可靠地触发ajax请求吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种监视用户编辑会话的方法,而我正在审核的解决方案之一就是让我使用卸载事件来发送一个ajax请求来通知服务器的编辑会话结束。 (请参阅:监控用户会话以防止编辑冲突

I need a method to monitor user edit sessions, and one of the solutions I'm reviewing will have me using an unload event to send an ajax request to inform the server of the end of the edit session. (See: Monitoring User Sessions to Prevent Editing Conflict)

我的(相当有限的)阅读 unload 事件表示附加到此处理程序的代码必须快速运行,并且这样通常用于清除对象以防止内存泄漏。

My (rather limited) reading on the unload event indicate that the codes attached to this handler has to run quickly, and as such is usually used for clearing objects to prevent memory leaks.

我的问题是,为了这个目的,这个工作是否足够可靠?

My question is, can this work reliably enough for this purpose?

PS。我知道 async:false 选项。

PS. I know about the async: false option.

推荐答案

如果您的服务器足够快速响应,此方法相当可靠。有些事情真的要注意。如果关闭浏览器并在卸载事件上发送AJAX请求,那么在窗口对象被破坏之前,响应将不会及时从服务器返回的机会很大。在这种情况下(至少与IE一起)会发生什么,它会使连接对象孤立,直到连接超时被击中才能正确终止它。如果您的服务器没有启用连接保持活动,则在关闭2个窗口(同时仍有另一个窗口打开)之后,将不再使用与服务器的打开连接(对于IE6-7,对于IE8 - 6个窗口)在您的连接超时被击中之前,您将无法打开您的网站。

This method is fairly reliable, if your server is fast enough to respond. Something to really watch out for though. If you close the browser and send AJAX request on unload event, there's a very good chance that the response isn't going to come back from the server in time before the window object is destroyed. What happens in this case (at least with IE) is that it will orphan your connection object and not terminate it correctly until the connection timeout is hit. If your server doesn't have connection keep-alive turned on, after you close 2 windows (while still having another window open), you will run out of open connections to the server (for IE6-7, for IE8 - 6 windows) and you will not be able to open your website until your connection timeout is hit.

在我正在打开一个正在发送的弹出窗口之前,遇到了一种情况一个AJAX请求卸载,这是非常可靠的,但它受到上述发布的困扰,我花了很长时间跟踪它,了解发生了什么。之后,我做了什么,我确保打开的窗口将具有相同的代码来调用服务器,每次卸载检查开启者,并运行代码,如果它存在。

I ran into a situation like that before were I was opening a popup window that was sending an AJAX request on unload, it was very reliable, but it was plagued by the issued described above, and it took really long time for me to track it down and understand what's going on. After that, what I did, is I made sure that opening window would have the same code to call server, and on every unload checked for the opener and ran the code there if it was present.

似乎如果您关闭最后一个浏览器窗口,IE将正确地销毁连接,但如果另一个窗口打开,则不会。

It seems that if you close the very last browser window, IE will destroy connection properly, but if one other window is open, it will not.

PS只是为了评论上面的答案,AJAX并不是真的异步。至少JS实现它不是。发送请求后,您的JS代码仍将等待服务器的响应。它不会阻止您的代码执行,但由于服务器可能需要一段时间来响应(或足够长的时间才能终止IE窗口对象),您可能会遇到上述问题。

P.S. And just to comment on the answer above, AJAX is not really async. At least JS implementation of it isn't. After you send a request, you JS code is still going to be waiting for response from the server. It's not going to block your code execution, but since the server might take a while to response (or long enough for Windows to terminate IE window object) you might and probably will run into the problem described above.

这篇关于可以使用卸载事件来可靠地触发ajax请求吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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