[英]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.