与POST的Response.Redirect,而不是得到什么? [英] Response.Redirect with POST instead of Get?

查看:235
本文介绍了与POST的Response.Redirect,而不是得到什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们必须采取一个表单提交并保存一些数据,然后将用户重定向到一个页面异地的要求,但在重定向,我们需要提交与POST形式,没有得到。

We have the requirement to take a form submission and save some data, then redirect the user to a page offsite, but in redirecting, we need to "submit" a form with POST, not GET.

我希望有一个简单的方法来做到这一点,但我开始觉得没有。我想我现在必须创建一个简单的其他网页,只有我想要的形式,重定向到它,填充表单变量,然后做一个body.onload调用一个脚本,只调用document.forms [0] .submit( );

I was hoping there was an easy way to accomplish this, but I'm starting to think there isn't. I think I must now create a simple other page, with just the form that I want, redirect to it, populate the form variables, then do a body.onload call to a script that merely calls document.forms[0].submit();

谁能告诉我,如果有别的选择吗?我们可能需要调整这个在项目后期,这可能会有点复杂,所以如果有一个简单的,我们能做到这一点的所有非其他网页依赖那将是梦幻般的。

Can anyone tell me if there is an alternative? We might need to tweak this later in the project, and it might get sort of complicated, so if there was an easy we could do this all non-other page dependent that would be fantastic.

无论如何,感谢任何和所有响应。

Anyway, thanks for any and all responses.

推荐答案

这样做需要了解如何HTTP重定向的工作。当您使用的Response.Redirect(),你送与的 HTTP状态code 302 ,它告诉浏览器下一步去哪里。根据定义,浏览器会作出这样通过 GET 的要求,即使原来的请求是一个 POST

Doing this requires understanding how HTTP redirects work. When you use Response.Redirect(), you send a response (to the browser that made the request) with HTTP Status Code 302, which tells the browser where to go next. By definition, the browser will make that via a GET request, even if the original request was a POST.

另一种选择是使用 HTTP状态code 307 ,该指定浏览器应该以同样的方式与原始请求的重定向请求,而是提示与安全警告用户。要做到这一点,你会这样写:

Another option is to use HTTP Status Code 307, which specifies that the browser should make the redirect request in the same way as the original request, but to prompt the user with a security warning. To do that, you would write something like this:

public void PageLoad(object sender, EventArgs e)
{
    // Process the post on your side   

    Response.Status = "307 Temporary Redirect";
    Response.AddHeader("Location", "http://example.com/page/to/post.to");
}

不幸的是,这不会总是有效。 不同的浏览器不同的实现这一点,因为它不是一个普通状态code。

Unfortunately, this won't always work. Different browsers implement this differently, since it is not a common status code.

唉,不像Opera和Firefox开发人员来说,IE开发者从未读过的规范,甚至是最新的,最安全的IE7将从域A重定向POST请求域B没有任何警告或确认对话框! Safari浏览器也充当一种有趣的方式,虽然它没有提出一个确认对话框并执行重定向,它扔掉POST数据,有效地改变307重定向到较常见的302。

Alas, unlike the Opera and FireFox developers, the IE developers have never read the spec, and even the latest, most secure IE7 will redirect the POST request from domain A to domain B without any warnings or confirmation dialogs! Safari also acts in an interesting manner, while it does not raise a confirmation dialog and performs the redirect, it throws away the POST data, effectively changing 307 redirect into the more common 302.

所以,据我所知,只有这样,才能实现这样的事情将是使用JavaScript。有两种选择我能想到把我的头顶部:

So, as far as I know, the only way to implement something like this would be to use Javascript. There are two options I can think of off the top of my head:


  1. 创建表格,有它的动作属性指向第三方服务器。然后,点击事件添加到提交按钮,首先执行一个AJAX请求到服务器的数据,然后允许的形式提交给第三方服务器。

  2. 创建张贴到你的服务器的形式。当提交表单时,向用户显示的页面中有一个形式与所有要传送的数据,都在隐藏的输入。只显示这样的消息重定向...。然后,一个JavaScript事件添加到将表单提交到第三方服务器的页面。

  1. Create the form and have it's action attribute point to the third-party server. Then, add a click event to the submit button that first executes an AJAX request to your server with the data, and then allows the form to be submitted to the third-party server.
  2. Create the form to post to your server. When the form is submitted, show the user a page that has a form in it with all of the data you want to pass on, all in hidden inputs. Just show a message like "Redirecting...". Then, add a javascript event to the page that submits the form to the third-party server.

在这两个,我会选择第二个,有两个原因。首先,它是比第一,因为它的工作的Javascript不需要更可靠;对于那些尚未启用谁也,你总是可以做出的隐藏表单提交可见按钮,并指示他们preSS它,如果它超过5秒。其次,你可以决定哪些数据被传输到第三方服务器;如果你只使用处理表单,因为它的推移,你会沿着所有的post数据,这并不总是你想要什么的路过。同为307的解决方案,假设它工作了所有用户。

Of the two, I would choose the second, for two reasons. First, it is more reliable than the first because Javascript is not required for it to work; for those who don't have it enabled, you can always make the submit button for the hidden form visible, and instruct them to press it if it takes more than 5 seconds. Second, you can decide what data gets transmitted to the third-party server; if you use just process the form as it goes by, you will be passing along all of the post data, which is not always what you want. Same for the 307 solution, assuming it worked for all of your users.

希望这有助于!

这篇关于与POST的Response.Redirect,而不是得到什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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