http.Redirect不起作用

huangapple go评论66阅读模式
英文:

http.Redirect not working

问题

我是你的中文翻译助手,以下是翻译好的内容:

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

对于路由器,我正在使用Mux:

router.HandleFunc("/login", pages.Login).Methods("POST")

Login函数包含以下代码:

if errorFlag {
    http.Redirect(rw, rq, "/", http.StatusNotAcceptable)
} else {
    http.Redirect(rw, rq, "/", http.StatusOK)
}

问题是,根据errorFlag,我得到了正确的状态码,但页面没有重定向!响应头中的"Location:/"也似乎设置正确。
但是,页面没有重定向,仍然停留在"/login"下。

我在Chrome和Firefox上进行了测试。

以下是响应头:

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)
}

谢谢!

英文:

i'm new to go, and i'm trying to do a redirect after login.

for the router, i'm using Mux:

router.HandleFunc("/login", pages.Login).Methods("POST")

and the Login func contains these lines:

if errorFlag {
    http.Redirect(rw, rq, "/", http.StatusNotAcceptable)
} else {
    http.Redirect(rw, rq, "/", http.StatusOK)
}

thing is, i'm getting the correct status according to the errorFlag, but the page is not redirected! the headers also seem to be set correctly ("Location:/")
but instead of redirecting, the page just stays blank and remains under "/login"

i've tested it on Chrome & FF.

these are the response 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

anyone every encountered this before?

Update

As suggested below, this change works:

if errorFlag {
	http.Redirect(rw, rq, "/", http.StatusTemporaryRedirect)
} else {
	http.Redirect(rw, rq, "/", http.StatusFound)
}

thanks!

答案1

得分: 6

使用3xx状态码来重定向客户端(http.StatusFoundhttp.StatusMovedPermanently,http.StatusSeeOther,...)。仅使用Location头部不足以引发重定向。

英文:

Use a 3xx status code to redirect the client (http.StatusFound, http.StatusMovedPermanently, http.StatusSeeOther, ...). The Location header is not sufficient to cause a redirect.

答案2

得分: 4

你正在尝试重定向POST方法,根据W3.org的规定,既不应该使用301(StatusMovedPermanently)也不应该使用302(StatusFound)。

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

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

英文:

You are trying to redirect POST method so neither 301 (StatusMovedPermanently) nor 302 (StatusFound) should work according to W3.org.

Try returning 303 (StatusSeeOther) if you want to redirect with GET method. Try returning status 307 (StatusTemporaryRedirect) if you want to redirect with the same method as in the request.

Details here: https://softwareengineering.stackexchange.com/questions/99894/why-doesnt-http-have-post-redirect

huangapple
  • 本文由 发表于 2016年1月15日 00:46:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/34795086.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定