jQuery的卸载与AJAX调用不工作在Chrome浏览器浏览器、工作、jQuery、AJAX

2023-09-11 01:27:06 作者:征途

这是我最简单的code片断:

This is my very simple code snippet:

$(window).unload(function() {
        $.ajax({
            url: 'stats_pages.php?last_id='+$("#last_id").val(),
        });
});

就这么简单。 Ajax调用被称为完美的Firefox,但无法在Chrome。我试过其他的变化,哪种类型的工作:

Simple enough. The AJAX call gets called perfectly in Firefox, but fails in Chrome. I've tried other variations, which sort of work:

window.onbeforeunload = function(){
        $.ajax({
            url: 'stats_pages.php?last_id='+$("#last_id").val(),
        });
        return false;
}

这工作在Chrome浏览器,但它提醒假与通常的你要离开这个页面你确定?消息,这不是我想要的效果显着。如果没有返回false; ,它不火的AJAX调用

This works in Chrome, but it alerts "false" with the usual "Are you sure you want to leave this page?" message, which is not what I want obviously. Without the return false;, it doesn't fire the AJAX call.

在理想情况下,我喜欢的第一个解决方案是最好的,但没有任何人知道这是怎么回事?

Ideally I like the first solution the best, but does anybody know what's going on?

推荐答案

设置异步:假的在你的Ajax调用

Set ASYNC : false in your ajax call

$(window).unload(function() {
        $.ajax({
            url: 'stats_pages.php?last_id='+$("#last_id").val(),
            async : false,
        });
});