如何在POST + HTTP重定向后向用户显示消息 [英] How to display messages to the user after a POST + HTTP redirect

查看:133
本文介绍了如何在POST + HTTP重定向后向用户显示消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PRG模式,以避免多次提交表单。然而,它有一个严重的缺点 - 你不能简单地向用户简单的 echo 确认消息(显然,用户不会看到页面,他将被重定向到另一个一个)。

I’m using the PRG pattern to avoid multiple form submission. It has, however, a serious drawback — you cannot simply echo the confirmation message to the user (obviously, the user won’t see the page, he will be redirected to another one).

这个问题的解决方案是什么?我知道他们中的两个,但没有一个似乎是完美的。

What are the solutions to this problem? I know two of them, but none of them seems perfect.


  • 使用自定义重定向网址,如: http ://example.com/味精=数据保存的。这是无国籍的,所以我认为这是非常可靠的。但是,当用户复制链接,将其加入书签等时,会产生问题。

  • 存储会话变量/ Cookie,并在每次加载页面时进行检查。如果设置,清除它并显示消息。似乎可以,但我不知道这一个 - 它强烈依赖饼干,这有点复杂。

  • Use a custom redirect URL, like: http://example.com/?msg=data-saved. It’s stateless, so I think it’s quite reliable. But it creates problems when user copies the link, bookmarks it, etc.
  • Store a session variable/cookie, and check it on every page load. If it’s set, clear it and display the message. It seems OK, but I’m not sure about this one — it relies strongly on cookies, it’s a little more complicated.

或者还有其他不了解的方法?会话和URL参数的组合? Dunno。

Or maybe there are other ways I don’t know about? Some combination of sessions and URL parameters? Dunno.

你认为最好的方法是什么?哪个缺点最少?什么是利弊?

What’s the best way in your opinion? Which one has the least drawbacks? What are the pros and cons?

推荐答案

几乎允许用户登录的每个网站都依赖于cookie。这不是一个完美的解决方案,但它是我们得到的最好的。

Pretty much every website that allows users to log in does so by relying on a cookie. It ain't a perfect solution, but it's the best we got.

另外,会话处理是Web开发框架通常会为您处理的事情之一。

Also session handling is one of those things that a web development framework typically takes care of for you.

这篇关于如何在POST + HTTP重定向后向用户显示消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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