在onunload处理jQuery的AJAX调用获得页面上手动刷新烧成后。我如何保证onunload先发生? [英] jQuery ajax call in onunload handler firing AFTER getting the page on a manual refresh. How do I guarantee onunload happens first?

查看:388
本文介绍了在onunload处理jQuery的AJAX调用获得页面上手动刷新烧成后。我如何保证onunload先发生?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,第卸载我射击删除的用户的工作草案的请求。这工作得很好,直到用户选择刷新页面,于是检查浏览器的网络选项卡中显示,Chrome的加载新的页面Ajax请求实际上是发射之前。其结果是由页面加载创建了一个新的草案,然后被删除马上通过Ajax调用。这基本上是我在做什么:

I have a situation where on page unload I am firing a request to delete the draft of a user's work. This works fine until a user chooses to refresh the page, whereupon inspecting Chrome's network tab shows that Chrome is loading the new page before the ajax request is actually fired. The result is a new draft is created by the page load, and then deleted right away by the ajax call. Here is basically what I'm doing:

window.onbeforeunload = function(){
    if (pageUpdated){
        return 'Are you sure you want to abandon this draft?';
    }
}

window.onunload = function(){
    $.ajax({
        async: false,
        type: 'DELETE',
        url: '/some/url/'
    });
}

有没有办法强制onunload处理程序,完成新页面的请求被发送之前?

Is there a way to force the onunload handler to complete before the request for the new page is sent?

浏览器测试:(铬9,FF 3.6)在Ubuntu 10.04清醒

Browsers Tested: (Chrome 9, FF 3.6) on Ubuntu 10.04 Lucid

推荐答案

在onbeforeunload的或$ .unload()设置与异步=虚假的Ajax对象。这将确保Ajax调用完成页面卸载之前。

In onbeforeunload or $.unload() set up your AJAX object with async = false. This will make sure the ajax call completes before the page is unloaded.

我知道这个问题是一个几个月大的,但我想添加这个信息的情况下,它可以帮助任何人搜索具有AJAX的类似问题!

I know this question is a few months old, but I wanted to add this info in case it helps anyone searching for a similar issue with AJAX!

这篇关于在onunload处理jQuery的AJAX调用获得页面上手动刷新烧成后。我如何保证onunload先发生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆