防止后退按钮显示 POST 确认警报

发布于 2024-07-15 23:30:15 字数 584 浏览 4 评论 0原文

我有一个向网页提供一长串参数的应用程序,因此我必须使用 POST 而不是 GET。 问题是,当页面显示并且用户单击“后退”按钮时,Firefox 会显示警告:

要显示此页面,Firefox 必须发送信息来重复之前执行的任何操作(例如搜索或订单确认)。

由于应用程序的构建方式使得返回是一种非常常见的操作,这对最终用户来说确实很烦人。

基本上,我想按照此页面的方式进行操作:

http://www.pikanya.net/testcache/

输入内容,提交,然后单击“后退”按钮。 没有任何警告,它只是返回。

谷歌搜索后我发现这可能是 Firefox 3 中的一个错误,但我想以某种方式得到这种行为,即使他们“修复”了它。

我猜想用一些 HTTP 标头就可以做到这一点,但到底是哪一个呢?

I have an application that supplies long list of parameters to a web page, so I have to use POST instead of GET. The problem is that when page gets displayed and user clicks the Back button, Firefox shows up a warning:

To display this page, Firefox must send information that will repeat any action (such as a search or order confirmation) that was performed earlier.

Since application is built in such way that going Back is a quite common operation, this is really annoying to end users.

Basically, I would like to do it the way this page does:

http://www.pikanya.net/testcache/

Enter something, submit, and click Back button. No warning, it just goes back.

Googling I found out that this might be a bug in Firefox 3, but I'd like to somehow get this behavior even after they "fix" it.

I guess it could be doable with some HTTP headers, but which exactly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

似最初 2024-07-22 23:30:15

请参阅我的网络编程黄金法则:

停止数据插入数据库两次

它说:“永远不要用正文响应 POST 请求。 始终完成工作,然后使用 Location: 标头进行响应,以重定向到更新的页面,以便浏览器使用 GET 请求它”

如果浏览器询问用户有关重新 POST 的信息,则您的 Web 应用程序将损坏。 用户不应该看到这个问题。

See my golden rule of web programming here:

Stop data inserting into a database twice

It says: “Never ever respond with a body to a POST-request. Always do the work, and then respond with a Location: header to redirect to the updated page so that browser requests it with GET”

If browser ever asks user about re-POST, your web app is broken. User should not ever see this question.

赠我空喜 2024-07-22 23:30:15

一种解决方法是将 POST 重定向到重定向到 GET 的页面 - 请参阅 Post /重定向/获取维基百科

假设您的 POST 是 4K 的表单数据。 据推测,您的服务器会对该数据执行某些操作,而不仅仅是将其显示一次然后将其丢弃,例如将其保存在数据库中。 继续这样做,或者如果它是一个巨大的搜索表单,请在数据库中创建它的临时副本,该副本会在几天后或在使用空间限制时基于 LRU 进行清除。 现在创建可以使用 GET 访问的数据表示形式。 如果是临时的,则为其生成一个 ID 并将其用作 URL; 如果它是一组永久数据,它可能有一个 ID 或可用于 URL 的内容。 在最坏的情况下,像tiny url 这样的算法可以将一个大URL 折叠成一个小得多的URL。 将 POST 重定向到 GET 数据的表示形式。


作为历史记录,这种技术是既定实践1995 年

One way round it is to redirect the POST to a page which redirects to a GET - see Post/Redirect/Get on wikipedia.

Say your POST is 4K of form data. Presumably your server does something with that data rather than just displaying it once and throwing it away, such as saving it in a database. Keep doing that, or if it's a huge search form create a temporary copy of it in a database that gets purged after a few days or on a LRU basis when a space limit is used. Now create a representation of the data which can be accessed using GET. If it's temporary, generate an ID for it and use that as the URL; if it's a permanent set of data it probably has an ID or something that can be used for the URL. At the worst case, an algorithm like tiny url uses can collapse a big URL to a much smaller one. Redirect the POST to GET the representation of the data.


As a historical note, this technique was established practice in 1995.

追星践月 2024-07-22 23:30:15

避免该警告/行为的一种方法是通过 AJAX 执行 POST,然后将用户单独发送到另一个页面(或不发送)。

One way to avoid that warning/behavior is to do the POST via AJAX, then send the user to another page (or not) separately.

时常饿 2024-07-22 23:30:15

我一直在使用 Session 变量来帮助解决这种情况。 这是我使用的方法,多年来一直对我很有效:

//If there's something in the POST, move it to the session and then redirect right back to where we are
if ($_POST) {
    $_SESSION['POST']=$_POST;
    redirect($_SERVER["REQUEST_URI"]);
}

//If there's something in the SESSION POST, move it back to the POST and clear the SESSION POST
if ($_SESSION['POST']) {
    $_POST=$_SESSION['POST'];
    unset($_SESSION['POST']);
}

从技术上讲,您甚至不需要将其放回名为 $_POST 的变量中。 但它可以帮助我跟踪数据来自哪里。

I have been using the Session variable to help in this situation. Here's the method I use that has been working great for me for years:

//If there's something in the POST, move it to the session and then redirect right back to where we are
if ($_POST) {
    $_SESSION['POST']=$_POST;
    redirect($_SERVER["REQUEST_URI"]);
}

//If there's something in the SESSION POST, move it back to the POST and clear the SESSION POST
if ($_SESSION['POST']) {
    $_POST=$_SESSION['POST'];
    unset($_SESSION['POST']);
}

Technically you don't even need to put it back into a variable called $_POST. But it helps me in keeping track of what data has come from where.

写下不归期 2024-07-22 23:30:15

我有一个应用程序向网页提供一长串参数,因此我必须使用 POST 而不是 GET。 问题是,当页面显示并且用户单击“后退”按钮时,Firefox 会显示警告:

您的推理是错误的。 如果请求没有副作用,应该是GET。 如果有副作用,应该是 POST。 选择不应基于您需要传递的参数数量。

I have an application that supplies long list of parameters to a web page, so I have to use POST instead of GET. The problem is that when page gets displayed and user clicks the Back button, Firefox shows up a warning:

Your reasoning is wrong. If the request is without side effects, it should be GET. If it has side effects, it should be POST. The choice should not be based on the number of parameters you need to pass.

青柠芒果 2024-07-22 23:30:15

作为另一种解决方案,您可以完全停止使用重定向。

您可以立即处理并呈现处理结果,而不会出现 POST 确认警报。 您应该只操作浏览器历史记录对象:

history.replaceState("", "", "/the/result/page")

请参阅完整简短回答

As another solution you may stop to use redirecting at all.

You may process and render the processing result at once with no POST confirmation alert. You should just manipulate the browser history object:

history.replaceState("", "", "/the/result/page")

See full or short answers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文