http.Redirect 不工作
http.Redirect not working
我是新来的,我正在尝试在登录后进行重定向。
对于路由器,我使用的是 Mux:
router.HandleFunc("/login", pages.Login).Methods("POST")
并且登录函数包含这些行:
if errorFlag {
http.Redirect(rw, rq, "/", http.StatusNotAcceptable)
} else {
http.Redirect(rw, rq, "/", http.StatusOK)
}
问题是,根据 errorFlag,我得到了正确的状态,但页面没有重定向! headers 似乎也设置正确 ("Location:/")
但页面没有重定向,而是保持空白并保留在“/login”
下
我已经在 Chrome 和 FF 上测试过了。
这些是回复 headers:
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
有人以前遇到过这个吗?
更新
如下所示,此更改有效:
if errorFlag {
http.Redirect(rw, rq, "/", http.StatusTemporaryRedirect)
} else {
http.Redirect(rw, rq, "/", http.StatusFound)
}
谢谢!
使用 3xx 状态代码重定向客户端(http.StatusFound, http.StatusMovedPermanently、http.StatusSeeOther、...)。位置 header 不足以导致重定向。
您正在尝试重定向 POST 方法,因此 301 (StatusMovedPermanently) 和 302 (StatusFound) 都不应该根据 W3.org 工作。
如果您想使用 GET 方法重定向,请尝试返回 303 (StatusSeeOther)。如果您想使用与请求中相同的方法进行重定向,请尝试返回状态 307 (StatusTemporaryRedirect)。
详情在这里:https://softwareengineering.stackexchange.com/questions/99894/why-doesnt-http-have-post-redirect
我是新来的,我正在尝试在登录后进行重定向。
对于路由器,我使用的是 Mux:
router.HandleFunc("/login", pages.Login).Methods("POST")
并且登录函数包含这些行:
if errorFlag {
http.Redirect(rw, rq, "/", http.StatusNotAcceptable)
} else {
http.Redirect(rw, rq, "/", http.StatusOK)
}
问题是,根据 errorFlag,我得到了正确的状态,但页面没有重定向! headers 似乎也设置正确 ("Location:/") 但页面没有重定向,而是保持空白并保留在“/login”
下我已经在 Chrome 和 FF 上测试过了。
这些是回复 headers:
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
有人以前遇到过这个吗?
更新
如下所示,此更改有效:
if errorFlag {
http.Redirect(rw, rq, "/", http.StatusTemporaryRedirect)
} else {
http.Redirect(rw, rq, "/", http.StatusFound)
}
谢谢!
使用 3xx 状态代码重定向客户端(http.StatusFound, http.StatusMovedPermanently、http.StatusSeeOther、...)。位置 header 不足以导致重定向。
您正在尝试重定向 POST 方法,因此 301 (StatusMovedPermanently) 和 302 (StatusFound) 都不应该根据 W3.org 工作。
如果您想使用 GET 方法重定向,请尝试返回 303 (StatusSeeOther)。如果您想使用与请求中相同的方法进行重定向,请尝试返回状态 307 (StatusTemporaryRedirect)。
详情在这里:https://softwareengineering.stackexchange.com/questions/99894/why-doesnt-http-have-post-redirect