Javascript 在卸载之前将 AJAX 发送到服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14929832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send AJAX to server beforeunload
提问by tim peterson
So supposedly starting at Firefox > 4, binding the window jQuery object to beforeunloaddoesn't work anymore.
因此,据说从 Firefox > 4 开始,将窗口 jQuery 对象绑定到beforeunload不再起作用。
What'd I'd like to do is submit an AJAX post to delete my server's memcache data.
我想要做的是提交 AJAX 帖子以删除我服务器的内存缓存数据。
When I refresh the only open tab, I can see that the beforeunloadevent is called in both firefox and chrome with the following code as evidenced by the console.log message, "firefox/NON-firefox delete". The problem is that I never see the console.log message "memcache delete" indicating that my server never saw the $.ajaxrequest.
当我刷新唯一打开的选项卡时,我可以看到beforeunload在 Firefox 和 chrome 中使用以下代码调用了该事件,如 console.log 消息“firefox/NON-firefox 删除”所证明的那样。问题是我从未看到 console.log 消息“memcache delete”表明我的服务器从未看到$.ajax请求。
I realize it is bad to do browser sniffing and that there is no difference between what's included in the if and else statements. I'm merely showing code for what I've tried unsuccessfully in Firefox.
我意识到进行浏览器嗅探是不好的,并且 if 和 else 语句中包含的内容没有区别。我只是展示了我在 Firefox 中尝试失败的代码。
Anyone have any ideas?
谁有想法?
$(window).bind('beforeunload', function(){
if(/Firefox[\/\s](\d+)/.test(navigator.userAgent) && new Number(RegExp.) >= 4) {
console.log('firefox delete');
memcacheDelete();
return null;
}
else {
console.log('NON-firefox delete');
memcacheDelete();
return null;
}
});
function memcacheDelete() {
$.ajax({
url: "/memcache/delete",
type: "post",
data:{},
success:function(){
console.log('memcache deleted');
}//success
}); //ajax
}
采纳答案by Jashwant
Ajax is asynchronous.
Ajax 是异步的。
When you refresh (or close)your browser, beforeunloadis being called. And it means as soon as beforeunloadis finished executing, page will refresh (or close).
当您刷新(或关闭)浏览器时,beforeunload正在调用。这意味着一旦beforeunload执行完成,页面将刷新(或关闭)。
When you do an ajax request, (since its asynchronous) javascript interpreter does not wait for ajax successevent to be executed and moves down finishing the execution of beforeunload.
当您执行 ajax 请求时,(因为它是异步的)javascript 解释器不会等待 ajaxsuccess事件被执行并向下移动完成beforeunload.
successof ajax is supposed to be called after few secs, but you dont see it as page has been refreshed / closed.
success应该在几秒钟后调用 ajax,但是您看不到它,因为页面已刷新/关闭。
Side note:
边注:
.success()method is deprecated and is replaced by the .done()method
.success()方法已弃用,并由.done()方法替换
回答by tim peterson
Just for sake of completion, here's what I did, thanks to @Jashwant for the guidance:
I noticed that this other SO Q&A suggested the same solution.
The KEY isthe async:true(false)in the $.ajaxcall below:
为了完成,这就是我所做的,感谢@Jashwant 的指导:我注意到另一个 SO Q&A 提出了相同的解决方案。
关键是在async:true(false)在$.ajax下面的呼叫:
$(window).bind('beforeunload', function(){
if(/Firefox[\/\s](\d+)/.test(navigator.userAgent) && new Number(RegExp.) >= 4) {
console.log('firefox delete');
var data={async:false};
memcacheDelete(data);
return null;
}
else {
console.log('NON-firefox delete');
var data={async:true};
memcacheDelete(data);
return null;
}
});
function memcacheDelete(data) {
$.ajax({
url: "/memcache/delete",
type: "post",
data:{},
async:data.async,
success:function(){
console.log('memcache deleted');
}//success
}); //ajax
}

