简体   繁体   English

在页面卸载之前调用 ajax

[英]Call ajax on before page unload

I'm trying to call an ajax before user leaving a page, this what i have done so far.我试图在用户离开页面之前调用 ajax,这是我到目前为止所做的。 But it doesn't even hit the ajax page.但它甚至没有命中 ajax 页面。 This is what i have done so far.这是我到目前为止所做的。

window.onbeforeunload = closeIt();
function closeIt()
{
    var key="save-draft";

            $.ajax({
                url: "app/ajax_handler.php",
                type:"GET",
                data:{key:key},
                success: function(data)  {
                    return data;
                }
            });

}

I Have tried this one also both failed in my case.我已经尝试过这个,在我的情况下也都失败了。

$( window ).unload(function() {});

The only way I think is to let the user know that it's a process on background with a confirm message, that will block the exit until user click on Accept or you've got the response.我认为唯一的方法是让用户知道这是一个带有确认消息的后台进程,这将阻止退出,直到用户单击接受或您得到响应。

Something like that:类似的东西:

 window.onbeforeunload = closeIt(); function closeIt() { /*var key="save-draft"; $.ajax({ url: "app/ajax_handler.php", type:"GET", data:{key:key}, success: function(data) { return data; } });*/ setTimeout(function() { return confirm("There is a process that isn't finished yet, you will lose some data. Are you sure you want to exit?"); }, 1000); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM