页面卸载/离开后如何发送AJAX请求?

页面卸载/离开后如何发送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.