到目前为止我有这个
window.addEventListener("beforeunload", function(e){
$.post('insert.php', {'postmessage':'CALL bor.update_records_exit(\'' + authUser + '\');'}, function(data) {
e.returnValue = null;
return null;
})
.fail(function(response) {
alert('Error: ' + response.responseText);
});
});
这是有效的,但不可靠。有时请求已发送,有时未发送,我不明白为什么。
我知道这与请求的同步性有关。我尝试改用 .ajax() 并将 async 属性设置为 false,但后来我意识到 async:false 已被弃用。
我需要做的就是在用户关闭浏览器或选项卡时发送最终插入查询...请帮助。