页面卸载/离开后如何发送AJAX请求? [英] How do I send an AJAX request upon page unload / leaving?
本文介绍了页面卸载/离开后如何发送AJAX请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
window.onbeforeunload = function() {
return $j.ajax({
url: "/view/action?&recent_tracking_id=" + $recent_tracking_id + "&time_on_page=" + getSeconds()
});
}
这是我所拥有的,但是它返回一个[object Object]警报
this what I have, but it returns an alert with [object Object]
我如何执行AJAX?
注意:当我什么都不返回时,服务器不会显示它正在接收ajax请求.
note: when I just don't return anything, the server doesn't show that it is receiving the ajax request.
推荐答案
您无需返回任何内容,只需触发ajax调用即可.
You don't need to return anything, just fire the ajax call.
window.onbeforeunload = function(e) {
e.preventDefault();
$j.ajax({ url: "/view/action?&recent_tracking_id=" + $recent_tracking_id + &time_on_page=" + getSeconds()
});
}
如果您使用的是jQuery,则可以尝试绑定到 .unload()事件.
If you are using jQuery, you can try binding to the .unload() event instead.
这篇关于页面卸载/离开后如何发送AJAX请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文