我有一個很奇怪的輸出...讓我後我的代碼首先然後我會解釋:Golang:重定向兩次,導致HTTP:多response.WriteHeader調用
在主要功能我宣佈
manageMux.HandleFunc("/info", info)
首先我登錄並從「/登錄」頁面「/」重定向:從當前頁面「/」
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)
}
}
然後我重定向到另一頁「/信息」(這隻有按鈕):
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)
}
}
}
最後,我做了一個模板,想顯示在客戶端:
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)
}
現在,奇怪的是,當我點擊頁面上的「/」按鈕,我得到了錯誤「http:multiple response.WriteHeader calls」。同時,在我的頁面底部顯示一個名爲「found」的鏈接(奇怪!),當我點擊「found」鏈接時,我將所有解析的模板打印在服務器端而不是網頁上。
有誰知道爲什麼...?以及如何解決錯誤和在客戶端網頁上打印的東西?謝謝!!!
請注意,您正在解析處理程序中的所有模板。這可能會拖累演出。 http://stackoverflow.com/questions/28451675/it-takes-too-much-time-when-using-template-package-to-generate-a-dynamic-web-p – nosequeldeebee