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

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

问题描述

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

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

我想到的唯一选择是通过 JavaScript 提交表单.

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

有什么想法吗?

推荐答案

你的意思不是很清楚,所以让我们来看几个场景:

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

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

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

Easy, just specify the target as the form action:

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

  • POST 提交成功后应该重定向用户

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

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

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

    有点棘手,但有三个选项:

    Slightly tricky, but three options:

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

    有关所有 HTTP 重定向选项的列表,请参见此处:http://en.wikipedia.org/wiki/Http_status_codes#3xx_Redirection

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

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

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