因此,我目前正在爲我的Go web應用程序編寫登錄和註冊功能,並試圖實現一項功能,如果您未填寫所需的表單字段「用戶名「」密碼「它會給你一個http.Error
,然後我試圖使它http.Redirect
但我發現重定向時發生此錯誤。 http: multiple response.WriteHeader calls
這是我的代碼..Golang http:multiple response.WriteHeader calls
//Check form submission
var u user
if req.Method == http.MethodPost {
un := req.FormValue("username")
p := req.FormValue("password")
//Checking to see if user filled out required fields.
if un == ""{
http.Error(w, "Please fill out required fields, you will be redirected shortly.", http.StatusForbidden)
time.Sleep(3000 * time.Millisecond)
//http.Redirect(w, req, "/" http.StatusSeeOther)
return
}else if p == "" {
http.Error(w, "Please fill out required fields, you will be redirected shortly.", http.StatusForbidden)
time.Sleep(3000 * time.Millisecond)
//http.Redirect(w, req, "/", http.StatusSeeOther)
return
}
c.Value = un
u = user{un, p}
dbUsers[c.Value] = u
http.Redirect(w, req, "/login", http.StatusSeeOther)
log.Println(dbUsers)
return
}
我知道,那是因爲if/else語句中的多個HTTP調用但我不能完全拿出一個替代。任何幫助將不勝感激!
顯示所有的請求處理程序。另一個頭寫入來自我們看不到的代碼。 –
你不能調用'http.Error'然後'http.Redirect',這可能是你的錯誤的根源。我不確定你是否故意爲這篇文章發表評論。 – squiguy