如何刷新网页而不让浏览器询问我是否要再次重新发送
我得到以下代码来刷新我的网页。如果我不使用 POST 提交任何内容,它会很好用,但如果我这样做,当我的网页刷新时,我会从浏览器收到一条消息(见下图),
location.reload(true);
我不是在寻找浏览器设置调整。我正在寻找替代代码来刷新而无需询问。
I got the following code to refresh my webpage. It works great if I don't submit anything with POST but if i do, i get a message from the browser when my webpage refreshes (see image below)
location.reload(true);
I'm not looking for the browser settings tweak. I'm looking for alternative code to refresh without asking.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是由于页面是通过
POST
而不是GET
请求的。刷新将重新提交POST
数据。您可以使用window.location = window.location.href;
强制获取。如果您希望允许人们通过浏览器控件重新加载页面,那么您需要实现 PRG 模式 在
POST
之后重定向到GET
。This is caused due to the page being requested by
POST
instead ofGET
. Refreshing will resubmit thePOST
data. You can force a get usingwindow.location = window.location.href;
.If you want to allow people to reload the page through their browser controls then you will need to implement the PRG pattern which redirects to a
GET
after aPOST
.这将请求页面而不是重新加载。
This will request for a page and not a reload.
GET 与 POST
解决方案:不要 POST。 GET 应该是幂等的(取决于环境变化),POST 应该是一种修改。
POST 之后,您应该立即执行 GET 以使用 POST 结果重新加载页面。然后就可以随意刷新了。
示例:
page_1.html
go.php:
page2.html:
GET vs. POST
The solution: don't POST. GET is supposed to be idempotent (up to environmental changes), POST is supposed to be a modification.
After the POST, you should be performing an immediate GET to reload the page with the result of the POST. Then you can refresh at will.
Example:
page_1.html
go.php:
page2.html:
这是由浏览器端控制的,而不是由您控制的(即您无法控制它)。如果您想向用户显示一个无需重新发布选项即可刷新的页面,则必须通过在发布后重定向到使用 GET 而不是 POST 检索的页面来完成此操作。
尝试 JavaScript 重定向。或者,使用 AJAX 发布信息,这意味着刷新会重新加载原始页面,而不是 ajax 发布的结果。
This is controlled on the browser end and not yours (ie you CAN'T control it). If you want to show the user a page that can be refreshed without the repost option, you'll have to do it via a redirect following the post to a page that you retrieve with a GET not a POST.
Try a javascript redirect. Or alternatively, POST the information using AJAX which will mean refresh reloads the original page, not the results of your ajax post.
您可以将数据保存到 window.name 并使用 window.location = window.location.href 技巧。当 onload 时,检查 window.name 中是否有数据,将其取回并从那里删除它。在这种情况下,我经常使用 window.name 作为“会话”存储。
you can save your data to window.name and use the window.location = window.location.href trick. and when onload, check if you have data in window.name, get it back and remove it from there. i use the window.name as "session" storage quite often for situations like this.
(我会说西班牙语,我会尽力而为)
就我而言,我有一个按钮可以在数据库中查找未处理的票证,因此,每次我想知道是否有新的票证时,我都需要按该按钮,我用下一个 javascript 函数解决了这个问题,
其中 10000 是之间的时间每次刷新(10秒)
如果你没有按钮,你可以创建一个带有ID和提交的隐藏按钮
(它不是那么优雅,但对我有用)
(I'm spanish speaker, I´ll do my best)
In my case, I have a button to looking for open tickets in a DB, so, i need to press that button every time that I wanna know if there are new ones, I resolved with the next javascript function
where 10000 is the time between every refresh, (10 seconds)
if you don't have a button, you can create a hidden one with an ID and a submit
(it's not so elegant, but works for me)