使用POST请求重定向的好方法? [英] A good way to redirect with a POST request?

查看:919
本文介绍了使用POST请求重定向的好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过POST请求将用户重定向到外部网站。



我唯一的选择是通过JavaScript提交表单。






有什么想法?

解决方案

目前尚不清楚您的意思,因此我们来看几个方案:


  1. 用户应该POST表单到您自己的服务器

    Easy,只需将目标指定为表单操作即可:

     < form action =http://someotherserver.commethod =post> 


  2. 用户应该在POST提交成功后重定向 p>

    方便,像往常一样接受和处理POST数据,然后用 302 303


  3. 用户应将POST数据发送到您的服务器,并且在验证之后,另一台服务器

    稍微有点棘手,但有三个选择:


    • 您的服务器接受POST数据,当用户等待响应时,您建立到另一个服务器的连接,发布数据,接收响应,然后向用户返回答案。

    • 你用 307 重定向来回答,这意味着用户应该在另一个地址尝试相同的请求。理论上这意味着浏览器应该将相同的数据发送到另一台服务器。我不太清楚这是多么的支持,但任何浏览器理解HTTP1.1应该能够做到这一点。 AFAIA在实践中并未经常使用。

      PS:规范指出,至少需要用户确认307 POST重定向。唉,显然这里没有浏览器坚持规范。 IE只是重复请求(所以它适用于你的目的),但Firefox,Safari和Opera似乎放弃了POST数据。因此,这项技术不幸是不可靠的。

    • 使用技巧#1与隐藏表单字段结合,在两者之间添加一个步骤。
    • >

请参阅此处以获取所有HTTP重定向选项的列表: http://en.wikipedia.org/wiki/Http_status_codes#3xx_Redirection


I need to redirect a user to an external site though a POST request.

The only option I figured out is to do it submit a form through JavaScript.


Any ideas?

解决方案

It's not quite clear what you mean, so let's take a few scenarios:

  1. User should POST form to a server other than your own

    Easy, just specify the target as the form action:

    <form action="http://someotherserver.com" method="post">
    

  2. User should be redirected after a successful POST submit

    Easy, accept and process the POST data as usual, then respond with a 302 or 303 redirect header.

  3. User should POST data to your server and, after validation, you want to POST that data to another server

    Slightly tricky, but three options:

    • Your server accepts the POST data and while the user waits for a response, you establish a connection to another server, POSTing the data, receiving a response, then return an answer to the user.
    • You answer with a 307 redirect, which means the user should attempt the same request at another address. Theoretically it means the browser should POST the same data to another server. I'm not quite sure how well supported this is, but any browser understanding HTTP1.1 should be able to do it. AFAIA it's not used that often in practice.
      PS: The specification says that a 307 POST redirect needs to be at least acknowledged by the user. Alas, apparently no browser is sticking to the spec here. IE simply repeats the request (so it works for your purposes), but Firefox, Safari and Opera seem to discard the POST data. Hence, this technique is unfortunately unreliable.
    • Use technique #1 combined with hidden form fields, adding one step in between.

See here for a list of all HTTP redirection options: http://en.wikipedia.org/wiki/Http_status_codes#3xx_Redirection

这篇关于使用POST请求重定向的好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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