HTTP response with redirect, but without roundtrip?

前端 未结 2 1234
名媛妹妹
名媛妹妹 2021-01-13 18:28

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

I would maybe do this:



        
2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-13 19:13

    HTTP/1.1 200 OK
    Location: /hello
    

    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.

    (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.)

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

    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.

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

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

提交回复
热议问题