dousi6087
2016-02-08 18:12
浏览 279
已采纳

Golang:两次重定向并导致http:多次响应。WriteHeader调用

I have a very weird output ... let me post my code first then I will explain:

Under main function I declared

manageMux.HandleFunc("/info", info)

first I log in and redirect from "/login" to page "/":

func login(w http.ResponseWriter, r *http.Request) {
    if r.Method == "GET" {
        t, err := template.ParseFiles("manage/login.html")
        checkError(err)
        t.Execute(w, nil)
    } else { //POST
        r.ParseForm()
        //do some authentications here
        http.Redirect(w, r, "/", http.StatusFound)
    }
}

Then I redirect to another page "/info" from current page "/" (which has only buttons):

func manage(w http.ResponseWriter, r *http.Request) {
    t, err := template.ParseFiles("manage/manage.html")
    checkError(err)
    t.Execute(w, nil)
    r.ParseForm()
    if r.Form["Button"] != nil { //to get only POST actions from buttons
        if r.Form["Button"][0] == "Log" {
            http.Redirect(w, r, "/info", http.StatusFound)
        } 
    }
}

At last, I made a template and would like to show on client side:

const tpl=`stuff inside`

type InfoDefault struct {
    //stuff inside
}

func info(w http.ResponseWriter, r *http.Request) {
    info := InfoDefault{
        //stuff inside
    }

    t, err := template.New("info").Parse(tpl)
    checkError(err)
    err = t.Execute(os.Stdout, info)
    checkError(err)
}

Now, the weird thing is, when I click the button on page "/", I got the error "http: multiple response.WriteHeader calls". At the same time a link called "found" shows up on the bottom of my page (weird!), and when I click the link "found", I got all my parsed template printed on the server side instead of webpage.

Does anyone know why...? And how to fix the error and print stuff on client webpage? Thank you!!!

图片转代码服务由CSDN问答提供 功能建议

我的输出很奇怪……让我先发布代码,然后再进行解释:

在我声明的主要功能下

  manageMux.HandleFunc(“ / info”,info)
   
 \  n 

首先我登录并从“ / login”重定向到页面“ /”:

  func login(w http.ResponseWriter,r * http.Request){  
 if r.Method ==“ GET” {
t,err:= template.ParseFiles(“ manage / login.html”)
 checkError(err)
 t.Execute(w,nil)
} else  {// POST 
 r.ParseForm()
 //在此处进行一些验证
 http.Redirect(w,r,“ /”,http.StatusFound)
} 
} 
  <  / pre> 
 
 

然后我从当前页面“ /”(只有按钮)重定向到另一个页面“ / info”:

  func manage(  w http.ResponseWriter,r * http.Request){
t,err:= template.ParseFiles(“ manage / manage.html”)
 checkError(err)
 t.Execute(w,nil)
 r。  ParseForm()
如果r.Form [“ Button”]!= nil {// 仅来自按钮的POST操作
,如果r.Form [“ Button”] [0] ==“ Log” {
 http.Redirect(w,r,“ / info”,http.StatusFound)
} 
}  
} 
   
 
 

最后,我制作了一个模板并想在客户端显示:

   const tpl =`stuff inside` 
 
type InfoDefault struct {
 // stuff inside 
} 
 
func info(w http.ResponseWriter,r * http.Request){
 info:= InfoDefault {
  //里面的东西
} 
 
t,错误:= template.New(“ info”)。Parse(tpl)
 checkError(err)
 err = t.Execute(os.Stdout,info)
  checkError(err)
} 
   
 
 

现在,奇怪的是,当我单击页面“ /”上的按钮时,出现了错误“ http: response.WriteHeader调用”。 同时,在页面底部显示一个名为“找到”的链接(很奇怪!),当我单击链接“找到”时,我将所有解析的模板打印在服务器端而不是网页上。

有人知道为什么吗?? 以及如何解决错误并在客户端网页上打印内容? 谢谢!!!

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dpsx99068 2016-02-08 23:27
    已采纳

    As JimB already pointed out: your server gets confused because there are different status codes associated with both writing to http.ResponseWriter and the redirect. You can't do both at the same time.

    I would actually like to expand more on how you can carry data over to the next page (assuming you are redirecting).

    Headers You can write some information to the request object and receive it on the destination page. Example:

    func myHandler(w http.ResponseWriter, r *http.Request) {
        w.Header().Set("My-Awesome-Header", "Rocks")
        ...
    }
    

    Session: You are talking about access control as far as I can see, and I think persisting user data is better done through a session. Example: you can use a database or a session handler like https://github.com/gorilla/sessions. Check out this thread: Best practice with sessions (gorilla/sessions).

    Cookies: I'm not sure what kind of front-end you are using, but storing non-sensitive data on the cookie could be an option? Nothing beats this one (it has real choc-chip cookies in the examples ;-) ): https://astaxie.gitbooks.io/build-web-application-with-golang/content/en/06.1.html.

    打赏 评论
  • dsa45664 2016-02-08 18:16

    In your manage handler, you're executing the template which will write to the http.ResponseWriter and trigger an http.StatusOK (200) status code. You can't redirect after that, since that requires sending a different response code.

    If you need to redirect, do it before executing the template.

    打赏 评论

相关推荐 更多相似问题