<分区>
我的主要目标和实际场景:如果两个用户共享相同的凭据,那么第二个用户无法登录系统,直到第一个已经登录的用户注销系统。
实际上,当用户关闭浏览器窗口时,我想在服务器数据库上设置 login_flag=0
。我在
$(window).on('unload', function(){ // ajax here with async:false });
或
$(window).on('beforeunload', function(){ // ajax here with async:false });
它工作正常。但在 Chrome 中,它会记录以下消息:
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/.
我再次用谷歌搜索相同的内容,并了解到在卸载事件中使用 Ajax 是令人沮丧的,并从 here 中找到了以下详细信息
简而言之,他们反对使用带有 async:false
的 ajax 和 window.unload
或 window.beforeUnload
方法。如果我从 Ajax 中删除 async
参数,那么它不会按预期工作。
如果我想在用户关闭浏览器窗口时在服务器端执行某些事件,正确的方法是什么?