How to redirect a user after login?

xuanbao · · 706 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hi!</p> <p>I&#39;m currently using http.Redirect in order to redirect a user to another page after logging in, registering, following another user, etc. The problem is, that is doesn&#39;t immediately redirect. It shows the HTTP status message first and the user only gets redirected if he clicks on the status messages. Is there a way to redirect without this message? Couldn&#39;t find anything and it looks really stupid and unprofessional that way.</p> <p>Thanks in advance!</p> <p>edit: SOLVED! I used status code 200 instead of 302</p> <hr/>**评论:**<br/><br/>TheSpreader: <pre><p>If you send a 302 (http.StatusFound) code it should redirect automatically.</p></pre>mko31: <pre><p>That solved my problem, thank you! I used 200. I forgot there were status codes for redirection. Thank you very much :)</p></pre>tgaz: <pre><p><code>Redirect</code> does set the <code>Location</code> header as expected: <a href="http://golang.org/src/net/http/server.go?s=42815:42883#L1509" rel="nofollow">http://golang.org/src/net/http/server.go?s=42815:42883#L1509</a></p> <p>Be sure to return one of the redirect status codes, otherwise browsers will ignore the <code>Location</code> header: <a href="https://en.wikipedia.org/wiki/URL_redirection#HTTP_status_codes_3xx" rel="nofollow">https://en.wikipedia.org/wiki/URL_redirection#HTTP_status_codes_3xx</a></p> <p>In this case, 307 or 302 sounds suitable, depending on what HTTP version you need to support.</p></pre>mko31: <pre><p><a href="https://www.reddit.com/r/golang/comments/3loqtq/how_to_redirect_a_user_after_login/cv83268" rel="nofollow">That fixed it</a> </p> <p>Thanks :)</p></pre>moinboin: <pre><p>The user is probably seeing the status printed at <a href="https://golang.org/src/net/http/server.go#L1516" rel="nofollow">https://golang.org/src/net/http/server.go#L1516</a>.</p> <p>There are a couple of reasons why the status message is displayed instead of an immediate redirect:</p> <ul> <li>The status code is not 301, 302 or some other appropriate status code.</li> <li>Another part of the application called <a href="http://godoc.org/net/http#ResponseWriter.WriteHeader" rel="nofollow">WriteHeader</a> (possibly indirectly) before the application called <a href="http://godoc.org/net/http#Redirect" rel="nofollow">Redirect</a>.<br/></li> </ul> <p>Use browser developer tools to insepect the headers and status code. If you do not see the Location header, then it&#39;s the second case.</p></pre>mko31: <pre><p><a href="https://www.reddit.com/r/golang/comments/3loqtq/how_to_redirect_a_user_after_login/cv83268" rel="nofollow">That fixed it</a> </p> <p>Thanks for taking the time to answer my question!</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

706 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传