douan4359 2015-02-25 15:34
浏览 79

用户关闭浏览器窗口时发送ajax [重复]

My Main GOAL and actual scenario: If two users share the same credentials, then the second user can not login into the system until the first one who is already logged in, log outs system.

Actually I want to set login_flag=0 on server database, when user closes the browser window. I googled and figured out to send an ajax request before either by

$(window).on('unload', function(){ // ajax here with async:false });

or

$(window).on('beforeunload', function(){ // ajax here with async:false });

It's working fine. But in Chrome, it logs the following message:

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check http://xhr.spec.whatwg.org/.

I have googled again for the same and came to know that it is discouraging to use Ajax on unload event, and found following details from here

enter image description here

in short, they are against the use of ajax with async:false with window.unload or window.beforeUnload method. If I remove async param from Ajax, then it's not working as intended.

What is the right approach if I want to do some activity on server side if user closes browser window?

</div>
  • 写回答

1条回答 默认 最新

  • dongshou7903 2015-02-25 15:41
    关注

    I remember to be in a similar situation once where I needed to perform an action on server side once user closed the window. However, my research came to same conclusion as yours that is discouraged to use any type of ajax call on window unload events. So, my final approach was to set a specific Cookie on client side in the window unload event, and when the same user visited that page, only then I performed my required action and deleted the cookie from server side.

    I know this is not exactly performing the action at the same time but that was the best approach which worked for me.

    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题