因此,我目前正在为我的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