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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2604530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:06:42  来源:igfitidea点击:

A good way to redirect with a POST request?

javascripthtmldjangoredirect

提问by RadiantHex

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?

有任何想法吗?

回答by deceze

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 302or 303redirect 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 307redirect, 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.
  1. 用户应将表单 POST 到您自己以外的服务器

    很简单,只需将目标指定为表单操作:

    <form action="http://someotherserver.com" method="post">
    
  2. 用户应该在成功的 POST 提交后重定向

    很简单,像往常一样接受和处理 POST 数据,然后用一个302303重定向头响应。

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

    有点棘手,但三个选项:

    • 您的服务器接受 POST 数据,当用户等待响应时,您建立到另一台服务器的连接,POST 数据,接收响应,然后将答案返回给用户。
    • 您使用307重定向回答,这意味着用户应该在另一个地址尝试相同的请求。理论上,这意味着浏览器应该将相同的数据 POST 到另一台服务器。我不太确定这有多受支持,但任何理解 HTTP1.1 的浏览器都应该能够做到。AFAIA 它在实践中并不经常使用。
      PS:规范说 307 POST 重定向至少需要得到用户的确认。唉,显然没有浏览器坚持这里的规范。IE 只是重复请求(因此它可以满足您的目的),但 Firefox、Safari 和 Opera 似乎丢弃了 POST 数据。因此,不幸的是,这种技术是不可靠的。
    • 使用技术#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

回答by BalusC

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,只需复制'n'paste'n'run它:

<!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 保护;)

回答by Wolph

Javascript is the only way (to do it automatically). You simply can't redirect a POSTrequest via standard httpmethods. Are you sure that GETisn't an option here?

Javascript 是唯一的方法(自动执行)。您根本无法POST通过标准http方法重定向请求。你确定这GET不是一个选项吗?

回答by Alex Jasmin

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 GETmethod.

使用表单可能是作为链接、HTTP 重定向的唯一选择,并且<meta http-equiv="refresh" >只会导致浏览器使用GET方法加载另一个 URL 。

You don't necessarily have to use JavaScript to submit a form though. 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.

不过,您不一定必须使用 JavaScript 来提交表单。如果某些用户交互是可以接受的,您可以使用带有某些<input type="hidden">字段的表单并让用户按下提交按钮。

You may also want to ensure that the page you're redirecting to doesn't already accept GET parameters. Some scriptsaccept both GET and POST indiscriminately.

您可能还想确保您重定向到的页面尚未接受 GET 参数。一些脚本不加区别地接受 GET 和 POST。