了解“发布/重定向/获取” [英] Understanding "post/redirect/get"

查看:126
本文介绍了了解“发布/重定向/获取”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解post / redirect / get的确切过程。我已经梳理了这个网站和网络了好几个小时,除了这里是概念以外,什么也找不到。它是如此简单,它不需要一步一步的教程?有人知道我可以用一个好的网站或书籍来找到一个例子或教程吗?

研究 POST -redirect - GET 看起来像这样:


  • 客户端获取带有表单的页面。

  • 表单 POST 到服务器。

  • 服务器执行操作,然后重定向到另一个页面。

  • 客户端遵循重定向。



例如,假设我们有这样的网站结构:


  • / posts (显示帖子列表和指向添加帖子的链接)


    • /< id> (查看特定帖子)

    • / create GET 方法,返回表单发布到自身;如果它是 POS T 请求,创建帖子并重定向到 /< id> 终端)




/ posts 本身并不与这个特定模式相关,所以我'/ b>

/ posts /< id> 可能会像这样执行:




  • 使用该ID在数据库中查找帖子。 使用该帖子的内容呈现模板。



/ posts / create 可以这样实现:




  • 如果请求是 GET 请求:


    • 显示一个空目标,目标设置为自身,方法设置为 POST


  • 如果请求是 POST 请求:


    • 验证
    • 如果所有字段都是有效的,那么
    • 如果有无效的字段,则再次显示带有错误的表单。
    • b

      • 将该信息添加到数据库中。 重定向至 / posts /< id> (其中< id> 是从调用数据库返回的)




I am having a very hard time understanding the exact process of "post/redirect/get". I have combed through this site and the web for several hours and cannot find anything other than "here's the concept". Is it so simple that it requires no step-by-step tutorial? Does anyone know of a good website or book I could use to find an example or tutorial?

解决方案

As you may know from your research, POST-redirect-GET looks like this:

  • The client gets a page with a form.
  • The form POSTs to the server.
  • The server performs the action, and then redirects to another page.
  • The client follows the redirect.

For example, say we have this structure of the website:

  • /posts (shows a list of posts and a link to "add post")
    • /<id> (view a particular post)
    • /create (if requested with the GET method, returns a form posting to itself; if it's a POST request, creates the post and redirects to the /<id> endpoint)

/posts itself isn't really relevant to this particular pattern, so I'll leave it out.

/posts/<id> might be implemented like this:

  • Find the post with that ID in the database.
  • Render a template with the content of that post.

/posts/create might be implemented like this:

  • If the request is a GET request:
    • Show an empty form with the target set to itself and the method set to POST.
  • If the request is a POST request:
    • Validate the fields.
    • If there are invalid fields, show the form again with errors indicated.
    • Otherwise, if all fields are valid:
      • Add the post to the database.
      • Redirect to /posts/<id> (where <id> is returned from the call to the database)

这篇关于了解“发布/重定向/获取”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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