Go 请求。提交表单时发布数组为空
我正在尝试用go处理一个简单的html表单。然而,我无法获得任何提交后的数据。r.表单映射始终为[]。不知道我错在哪里 提前谢谢 这是密码Go 请求。提交表单时发布数组为空,go,Go,我正在尝试用go处理一个简单的html表单。然而,我无法获得任何提交后的数据。r.表单映射始终为[]。不知道我错在哪里 提前谢谢 这是密码 主程序包 进口( “html/模板” “日志” “net/http” ) func rootHandler(w http.ResponseWriter,r*http.Request){ t、 =template.New(“form.html”).Parse(form) t、 执行(w,“”) } func formHandler(w http.Respons
主程序包
进口(
“html/模板”
“日志”
“net/http”
)
func rootHandler(w http.ResponseWriter,r*http.Request){
t、 =template.New(“form.html”).Parse(form)
t、 执行(w,“”)
}
func formHandler(w http.ResponseWriter,r*http.Request){
log.Println(r.Form)
rootHandler(w,r)
}
func main(){
http.HandleFunc(“/”,rootHandler)
http.HandleFunc(“/login”,formHandler)
http.listendserve(“127.0.0.1:9999”,无)
}
变量形式=`
登录
`
看起来您需要先调用ParseForm。从go docs开始
// Form contains the parsed form data, including both the URL
// field's query parameters and the POST or PUT form data.
// This field is only available after ParseForm is called.
// The HTTP client ignores Form and uses Body instead.
Form url.Values
和一些代码,让你的例子工作
func formHandler(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
//handle error http.Error() for example
return
}
log.Println(r.Form)
rootHandler(w, r)
}
谢谢我正在关注撰写web应用程序的文章。它可能有点过时,并且没有提到ParseForm()。该示例使用FormValue方法,如果需要,该方法将调用ParseForm。您直接访问表单变量,因此必须直接调用ParseForm。
func formHandler(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
//handle error http.Error() for example
return
}
log.Println(r.Form)
rootHandler(w, r)
}