简体   繁体   English

用POST请求重定向的好方法吗?

[英]A good way to redirect with a POST request?

I need to redirect a user to an external site though a POST request. 我需要通过POST请求将用户重定向到外部站点。

The only option I figured out is to do it submit a form through JavaScript. 我发现的唯一选择是通过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 成功提交POST后应重定向用户

    Easy, accept and process the POST data as usual, then respond with a 302 or 303 redirect header. 像往常一样轻松,接受并处理POST数据,然后使用302303重定向标头进行响应。

  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. 您的服务器接受POST数据,并且在用户等待响应时,您将建立与另一台服务器的连接,POST数据,接收响应,然后将答案返回给用户。
    • You answer with a 307 redirect, which means the user should attempt the same request at another address. 您用307重定向回答,这意味着用户应在另一个地址尝试相同的请求。 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. 我不太确定这支持的程度如何,但是任何了解HTTP1.1的浏览器都应该能够做到。 AFAIA it's not used that often in practice. AFAIA在实践中并不经常使用。
      PS: The specification says that a 307 POST redirect needs to be at least acknowledged by the user. PS:规范说,至少需要用户确认307 POST重定向。 Alas, apparently no browser is sticking to the spec here. las,显然,没有浏览器遵守此处的规范。 IE simply repeats the request (so it works for your purposes), but Firefox, Safari and Opera seem to discard the POST data. IE只是重复请求(因此可以满足您的目的),但是Firefox,Safari和Opera似乎会丢弃POST数据。 Hence, this technique is unfortunately unreliable. 因此,不幸的是,该技术是不可靠的。
    • Use technique #1 combined with hidden form fields, adding one step in between. 将技巧1与隐藏的表单字段结合使用,在两者之间增加了一个步骤。

See here for a list of all HTTP redirection options: http://en.wikipedia.org/wiki/Http_status_codes#3xx_Redirection 有关所有HTTP重定向选项的列表,请参见此处: http : //en.wikipedia.org/wiki/Http_status_codes#3xx_Redirection

Just set HTML form's action URL to the particular external site. 只需将HTML表单的操作URL设置到特定的外部站点即可。

Here's an SSCCE , just copy'n'paste'n'run it: 这是一个SSCCE ,只需复制“ 粘贴 ”即可运行它:

<!doctype html>
<html lang="en">
    <head>
        <title>SO question 2604530</title>
    </head>
    <body>
        <form action="http://stackoverflow.com/questions/2604530/answer/submit" method="post">
            <textarea name="post-text"></textarea>
            <input type="submit" value="Post Your Answer">
        </form>
    </body>
</html>

You'll see that Stackoverflow has good CSRF protection ;) 您会看到Stackoverflow具有良好的CSRF保护;)

Javascript is the only way (to do it automatically). Javascript是唯一的方法(自动执行)。 You simply can't redirect a POST request via standard http methods. 您根本无法通过标准的http方法重定向POST请求。 Are you sure that GET isn't an option here? 您确定这里没有GET选项吗?

Using a form is probably your only option as links, HTTP redirects and <meta http-equiv="refresh" > will only cause the browser to load another URL using the GET method. 使用表单可能是您唯一的选择,因为链接,HTTP重定向和<meta http-equiv="refresh" >只会导致浏览器使用GET方法加载另一个URL。

You don't necessarily have to use JavaScript to submit a form though. 不过,您不一定必须使用JavaScript来提交表单。 If some user interaction is acceptable you could use a form with some <input type="hidden"> fields and let the user press the submit button. 如果可以接受某些用户交互,则可以使用带有某些<input type="hidden">字段的表单,然后让用户按下“提交”按钮。

You may also want to ensure that the page you're redirecting to doesn't already accept GET parameters. 您可能还需要确保重定向到的页面尚未接受GET参数。 Some scripts accept both GET and POST indiscriminately. 一些脚本不加选择地接受GET和POST。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM