[英]Browser refresh sends the last $http call made
AngularJS 1.2.13 AngularJS 1.2.13
var httpdelete = $http.delete("/api/categories/" + id);
httpdelete.success(function(data){
alert("Success");
});
httpdelete.error(function(data, status, header, config){
alert("Error!");
});
I do an asynchronous $http.delete
request 我做了一个异步$http.delete
请求
The success
callback function is executed and the alert box "success" appears. 执行success
回调函数,并显示警告框“成功”。
I hit the browser refresh button 我点击了浏览器的刷新按钮
The $http.delete
line is not executed (debugged with break points). $http.delete
行未执行(已用断点调试)。 Instead the error callback function is immedialy executed. 而是立即执行错误回调函数。 The alert box "error" appears. 出现警告框“错误”。 No request made it to the server after clicking on the browser's refresh button 单击浏览器的刷新按钮后,没有请求发送到服务器
I was expecting the entire page to reload when I hit the browser's refresh button. 当我按下浏览器的刷新按钮时,我期望整个页面都可以重新加载。
Instead, AngularJS seems to attempt to resend my last delete query without having to execute $http.delete
and goes straight to the error
callback. 相反,AngularJS似乎尝试重新发送我的上一个删除查询,而不必执行$http.delete
并直接进入error
回调。
How can I restore the natural behaviour of the browser's refresh button? 如何恢复浏览器刷新按钮的自然行为? I want it to reload the entire page and not attempt to resend the last asynchronous http request. 我希望它重新加载整个页面,而不是尝试重新发送最后一个异步http请求。
Open the network tab of the chrome dev tools. 打开chrome开发工具的“网络”标签。 Load your page and hit F5. 加载页面并点击F5。 If you don't see a get
to your index.html
(or whatever your base url is), it's because angular handled it. 如果你没有看到get
到您index.html
(或任何您的基本网址),这是因为角度来处理它。 If you do see the get
, the you have rebooted the app for real. 如果您确实看到get
,那么您已经真正重启了应用程序。
Once you know which one it is, you can investigate further. 一旦知道是哪一个,就可以进行进一步调查。 Setting a breakpoint in the httpdelete
callback and inspecting the callstack might also help. 在httpdelete
回调中设置断点并检查调用httpdelete
也可能会有所帮助。
Okay so here is what happened, my backend Nodejs+Express+MongoDB delete action was not returning anything to the client (browser). 好的,这就是发生的事情,我的后端Nodejs + Express + MongoDB删除操作未将任何内容返回给客户端(浏览器)。 I didn't think it was necessary to return any information after deleting the document from mongodb. 从mongodb删除文档后,我认为没有必要返回任何信息。
The side effect of that is as I described in the original post. 副作用是我在原始帖子中所描述的。 After deleting the document on the server, if a user refreshes the page using the browser refresh button then the page is not refreshed. 在服务器上删除文档后,如果用户使用浏览器刷新按钮刷新页面,则不会刷新页面。 Instead the $http.delete
request is resent to the server and on top of it the error callback is executed. 而是将$http.delete
请求重新发送到服务器,并在其之上执行错误回调。
After modifying my server side action and make it return a json document such as { success: true, message: "" }
after a delete request, the browser's refresh button behaves as it should have which is to reload the entire single application page index.html
. 修改我的服务器端操作并使其在删除请求后返回json文档,例如{ success: true, message: "" }
之后,浏览器的刷新按钮将按应有的方式工作,以重新加载整个单个应用程序页面index.html
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.