防止表单重新提交 [英] Preventing form resubmission

查看:135
本文介绍了防止表单重新提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一页包含一个HTML表单。第二页 - 处理提交数据的代码。



第一页中的表格被提交。浏览器被重定向到第二页。第二页处理提交的数据。



此时,如果第二页被刷新,会弹出一个确认表单重新提交提醒。



这可以防止?

解决方案

有两种方法人们习惯在这里采取:



方法1:使用AJAX +重定向



通过这种方式,您可以使用JQuery或类似于Page2 ,而用户仍然看到page1显示。成功发布后,您将浏览器重定向到Page2。

方法2:发布+重定向到自己

这是一种常用技术在论坛上。 Page1上的表单将数据发布到第2页,第2页处理数据并完成需要完成的工作,然后它自己进行HTTP重定向。通过这种方式,浏览器记得的最后一个动作是第2页上的一个简单GET,所以表单不会在F5上重新提交。

Page one contains an HTML form. Page two - the code that handles the submitted data.

The form in page one gets submitted. The browser gets redirected to page two. Page two handles the submitted data.

At this point, if page two gets refreshed, a "Confirm Form Resubmission" alert pops up.

Can this be prevented?

解决方案

There are 2 approaches people used to take here:

Method 1: Use AJAX + Redirect

This way you post your form in the background using JQuery or something similar to Page2, while the user still sees page1 displayed. Upon successful posting, you redirect the browser to Page2.

Method 2: Post + Redirect to self

This is a common technique on forums. Form on Page1 posts the data to Page2, Page2 processes the data and does what needs to be done, and then it does a HTTP redirect on itself. This way the last "action" the browser remembers is a simple GET on page2, so the form is not being resubmitted upon F5.

这篇关于防止表单重新提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆