为什么在window.unonload Ajax调用从来不叫? [英] Why is ajax call in window.unonload never called?

查看:232
本文介绍了为什么在window.unonload Ajax调用从来不叫?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写在Angularjs上载图。我希望确保上传过程中,如果该页面的重新加载关闭;该上传到服务器的所有文件应的一致性除去。那么,我想是因为以下几点:

I have an upload view written in Angularjs. I want to ensure that during an upload if the page is reloaded or closed; all files that uploaded to the server should be removed for consistency. So what I tried is as following:

// this function is under /upload view, and inside UploadController
window.onunload = function () {
   $http.delete(...call delete on my server..);
};

howevever,DELETE没有达到服务器,我缺少什么?

howevever, DELETE does not reach to server, what am I missing?

推荐答案

我以前也曾经有过这个问题。打开浏览器控制台,并转至网络选项卡。现在,导航离开该页面。你会看到一个快速的时刻,你的Ajax请求被标记为(取消),因为浏览器将取消该请求你搬走。

I've had this issue before too. Open up the browser console and go to the network tab. Now navigate away from the page. You will see for a quick moment that your ajax request is marked as (canceled) because the browser cancels the request as you move away.

我观察到在谷歌浏览此行为,并没有检查其他浏览器,但如果它不能在最流行的浏览器之一可靠地工作,那么你知道这是不是一个全面可靠的解决方案。

I've observed this behaviour in google chrome and haven't checked other browsers, but if it doesn't work reliably in one of the most popular browsers then you know this isn't a reliable solution overall.

基本上,没有可靠的方法来做到这一点。我发现,即使不这样做的 window.onbeforeunload Ajax调用是可靠的。

Basically, there is no reliable way to do this. I've found not even doing the ajax call in window.onbeforeunload is reliable.

如果我是你,我会把到期日在你上传的文件,并有删除它们达到期满后cron作业。即使 window.onunload 工作,它仍然不会是一个可靠的解决方案,例如,当用户的机器意外关闭。

If I were you, I would put an expiry date on your uploaded files and have a cron job that deletes them after the expiry date is reached. Even if window.onunload worked, it still wouldn't be a reliable solution—for example, when a user's machine unexpectedly shuts down.

这篇关于为什么在window.unonload Ajax调用从来不叫?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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