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

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

问题描述

我需要一种方法来监视用户编辑会话,我正在审查的其中一个解决方案将让我使用 unload 事件发送 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?

附注.我知道 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.

附言只是为了评论上面的答案,AJAX 并不是真正的异步.至少它的 JS 实现不是.发送请求后,您的 JS 代码仍将等待服务器的响应.它不会阻止您的代码执行,但由于服务器可能需要一段时间来响应(或足够长的时间让 Windows 终止 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天全站免登陆