简体   繁体   English

用户刷新页面时,将重新提交HTML表单-如何阻止这种情况发生?

[英]HTML form is resubmitted when user refreshes page - how do I stop this from happening?

After a user submits an html form (with method="post") it brings them back to the page they were on (but with some of the data changed). 用户提交html表单(使用method =“ post”)后,会将其带回到他们所在的页面(但某些数据已更改)。 If the user refreshes the page, the form is submitted again. 如果用户刷新页面,则会再次提交表单。 In my application this can create an illegal state (since the form data is submitted without going through verification). 在我的应用程序中,这可能会创建一个非法状态(因为表单数据是在未经验证的情况下提交的)。

How can I prevent the form from being submitted when the user refreshes the page? 当用户刷新页面时,如何防止提交表单?

Here is some sample code to give you a basic idea of what is going on: 以下是一些示例代码,可让您基本了解发生的情况:

<script type="javascript">
function verifyForm() {
//do stuff
}
...
<form onsubmit="return verifyForm();" method="post">
<input name="testing" type="text" size="8" />
<input name="save" type="submit value="Submit Form" />
</form>

Then in the JSP controller: 然后在JSP控制器中:

public class myController implements Controller
{ 
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception
{
// do stuff with the form data received in the request
}
}

There are several places I might be able to solve this problem: in the html page itself, or in the controller (using MVC pattern in jsp). 我可能有几个地方可以解决此问题:在html页面本身中,或在控制器中(在jsp中使用MVC模式)。 So far I have found this question that almost seems to apply to my question, but doesn't quite seem to be asking the same thing (also I am not using PHP). 到目前为止,我已经发现这个问题似乎几乎适用于我的问题,但是似乎并没有问同样的事情(也是我没有使用PHP)。

You need to use the PRG pattern: Post/Redirect/Get. 您需要使用PRG模式:Post / Redirect / Get。

Read about it here: http://en.wikipedia.org/wiki/Post/Redirect/Get 在此处阅读有关内容: http : //en.wikipedia.org/wiki/Post/Redirect/Get

The canonical solution is to redirect after every successful POST. 规范的解决方案是在每次成功的POST之后重定向。 The browser will perform the redirect using GET, so nothing gets posted; 浏览器将使用GET执行重定向,因此不会发布任何内容; if you reload, only the GET request, not the initial POST, will be repeated. 如果您重新加载,将仅重复GET请求,而不是初始POST。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在请求从 .NET 服务返回之前用户刷新页面时停止显示 InvalidOperationException? - How to stop InvalidOperationException from showing when user refreshes the page before the request comes back from .NET Service? HTML表单 - 当我点击输入时刷新页面! - HTML form - when I hit enter it refreshes page! 每次页面刷新时,如何将JavaScript文件中的随机图像加载到HTML中? - How do I load a random image from an array in a JavaScript file into HTML every time the page refreshes? 如何在用户刷新页面时阻止函数触发? - How to prevent a function from firing off when the user refreshes a page? 如何阻止 javascript 动画重复发生? - How do I stop a javascript animation from happening repeatedly? 为什么刷新页面后会自动重新提交表单 - Why a form would automatically resubmitted when the page is refreshed 如何在x秒后自动重新加载页面并重新提交表单数据 - How to automatically reload a page after x seconds with form data resubmitted 在分页页面上,如何阻止javascript自动加载? - How do I stop javascript from autoloading when on a pagination page? 如何在页面呈现时停止 onClick 呈现? - How do I stop onClick from rendering when the page renders? 当用户刷新整个页面时,阻止bootstrap 3模态关闭 - prevent bootstrap 3 modal from closing when user refreshes the entire page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM