http.Redirect 不起作用

Dea*_*ano 4 header http go

我是新手,我正在尝试在登录后进行重定向。

对于路由器,我使用 Mux:

router.HandleFunc("/login", pages.Login).Methods("POST")
Run Code Online (Sandbox Code Playgroud)

登录函数包含以下几行:

if errorFlag {
    http.Redirect(rw, rq, "/", http.StatusNotAcceptable)
} else {
    http.Redirect(rw, rq, "/", http.StatusOK)
}
Run Code Online (Sandbox Code Playgroud)

事情是,我根据 errorFlag 获得了正确的状态,但页面未重定向!标题似乎也设置正确(“位置:/”),但页面没有重定向,而是保持空白并保留在“/登录”下

我已经在 Chrome 和 FF 上测试过了。

这些是响应标头:

Content-Length:0
Content-Type:text/plain; charset=utf-8
Date:Thu, 14 Jan 2016 16:52:34 GMT
Location:localhost:8000/
Set-Cookie:user=MTQ1Mjc5MDM1N...; Path=/; Expires=Sat, 13 Feb 2016 16:52:34 UTC; Max-Age=2592000
Run Code Online (Sandbox Code Playgroud)

以前有人遇到过这个吗?

更新

如下所示,此更改有效:

if errorFlag {
    http.Redirect(rw, rq, "/", http.StatusTemporaryRedirect)
} else {
    http.Redirect(rw, rq, "/", http.StatusFound)
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

Cer*_*món 7

使用 3xx 状态代码重定向客户端(http.StatusFoundhttp.StatusMovedPermanently、 http.StatusSeeOther 等)。Location 标头不足以引起重定向。


kop*_*zko 5

您正在尝试重定向 POST 方法,因此 301 (StatusMovedPermanently) 和 302 (StatusFound) 都不应该根据 W3.org 工作。

如果您想使用 GET 方法进行重定向,请尝试返回 303 (StatusSeeOther)。如果您想使用与请求中相同的方法进行重定向,请尝试返回状态 307 (StatusTemporaryRedirect)。

详细信息此处: https: //softwareengineering.stackexchange.com/questions/99894/why-doesnt-http-have-post-redirect