带有重定向的HTTP响应,但没有往返? [英] HTTP response with redirect, but without roundtrip?

查看:160
本文介绍了带有重定向的HTTP响应,但没有往返?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望浏览器能够反映除用于创建请求的URL之外的其他URL,但不会向服务器进行往返。

I want the browser to reflect some other URL than the one used to create the request, but without roundtripping to the server.

我可能会这样做:

POST /form HTTP/1.1
...

...然后返回:

HTTP/1.1 200 OK
Location: /hello

但这会导致重定向,浏览器会再次出现,请求URL /你好。

But that would cause a redirect, the browser will again, request URL /hello.

我想告诉浏览器,你刚刚发送的请求是POST / some_url,我现在返回的实际资源实际上叫做GET / hello / 1但没有预先进行往返。即位置:...

I would like to just tell the browser that, while the request you just sent was POST /some_url the actuall resource that I'm now returning is actually called GET /hello/1 but without preforming a roundtrip. i.e. Location: ...

有没有办法用JavaScript或base =属性做到这一点?这会告诉浏览器在我点击F5(刷新)而不是发布提交警告时请求/ hello / 1?

Is there any way to do this with JavaScript or the base="" attribute? That will tell the browser to request /hello/1 when I hit F5 (refresh) instead of that, post submission warning?

推荐答案

HTTP/1.1 200 OK
Location: /hello



<实际上,这可能不起作用;它应该是30x状态而不是200(303 See Other最适合POST的响应),Location应该是一个完整的绝对URL。

Actually that probably wouldn't work; it should be a 30x status rather than 200 ("303 See Other" is best for the response to a POST), and ‘Location’ should be a complete absolute URL.

(如果您的脚本只是说'Location:/ relativeurl'没有30x状态,CGI服务器通常会通过获取新URL并返回它而不告诉浏览器发生任何有趣的事情来进行内部重定向。这可能听起来像你想要的但是这不是真的,因为从浏览器的角度来看,它与返回200和直接页面的原始脚本没有什么不同。)

(If your script just says ‘Location: /relativeurl’ without the 30x status, CGI servers will usually do an internal redirect by fetching the new URL and returning it without telling the browser anything funny happened. This may sound like what you want but it isn't really because from the browser's point of view it's no different from the original script returning a 200 and direct page.)


但是这将导致重定向,浏览器将再次请求URL / hello。

But that would cause a redirect, the browser will again, request URL /hello.

在实践中,由于HTTP / 1.1保持活动,这可能没有您想象的那么糟糕。客户端应该能够立即响应重定向(在下一个数据包中),只要它在同一台服务器上。

In practice that's probably not as bad as you think, thanks to HTTP/1.1 keep-alives. The client should be able to respond to the redirect straight away (in the next packet) as long as it's on the same server.


是有什么办法[...]当我点击F5(刷新)而不是发布提交警告时,会告诉浏览器请求/ hello / 1?

Is there any way [...] That will tell the browser to request /hello/1 when I hit F5 (refresh) instead of that, post submission warning?

不。坚持使用POST-Redirect-GET模型来解决这个问题。

Nope. Stick with the POST-Redirect-GET model for solving this.

这篇关于带有重定向的HTTP响应,但没有往返?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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