Html 停止浏览器要求在刷新时重新发送表单数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4327236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Stop browsers asking to resend form data on refresh?
提问by Bojangles
I have a form submission via POST. I submit the form, and all is well, however if I try to reload the new page that the form goes to after submission, I get the "Do you want to resend data" message (FireFox). It might happen in other browsers too, but I'm not sure.
我有一个通过 POST 提交的表单。我提交了表单,一切都很好,但是如果我尝试重新加载表单在提交后转到的新页面,我会收到“您想重新发送数据吗”消息(FireFox)。它也可能发生在其他浏览器中,但我不确定。
Does anyone know of a way to stop this message popping up so I can go ahead and refresh the page. It's not good for production environments - users may submit the same form twice!
有谁知道一种方法可以阻止此消息弹出,以便我可以继续刷新页面。这对生产环境不利 - 用户可能会提交相同的表单两次!
回答by SLaks
You need to use the the POST-Redirect-GET pattern.
您需要使用 POST-Redirect-GET 模式。
Make your form respond with a redirect to a GET request.
This way, when the user refreshes the page, it will only re-send the GET.
让您的表单通过重定向到 GET 请求来响应。
这样,当用户刷新页面时,它只会重新发送 GET。
回答by Merboy
An easy way after response.sendRedirect
is reload the page in this way:
一种简单的方法response.sendRedirect
是以这种方式重新加载页面:
window.location.href = window.location.pathname;
It works for me.
这个对我有用。
回答by Ashwin
If url does not have any parameter, use this -
如果 url 没有任何参数,请使用此 -
window.location = window.location.href+"?rnd="+Math.random();
Or else use this -
或者使用这个 -
window.location = window.location.href+"&rnd="+Math.random();
It will work.
它会起作用。
回答by Madhur Ahuja
It will be a security violation if browsers do that. For ex in credit card processing, the site may charge the user twice.
如果浏览器这样做,将违反安全。对于前信用卡处理,该网站可能会向用户收取两次费用。
The only way round is go to address bar and hit enter. It will cause a new request.
唯一的方法是转到地址栏并按回车键。它会导致一个新的请求。