我无法从HTML表单获取数据。该模板显示在localhost:3000处,我提交后被带到localhost:3000 / results,结果为“ 404页面未找到”。该URL不包含任何表单字段。
package main
import (
"html/template"
"net/http"
"github.com/go-martini/martini"
)
func main() {
m := martini.Classic()
m.Get("/", func(res http.ResponseWriter, req *http.Request) { // res and req are injected by Martini
t, _ := template.ParseFiles("form.gtpl")
t.Execute(res, nil)
})
m.Get("/results", func(r *http.Request) string {
text := r.FormValue("text")
return text
})
m.Run()
}
模板是:form.gtpl
<html>
<head>
<title>Title Here</title>
</head>
<body bgcolor="#E6E6FA">
<h1>Header Here</h1>
<form action="/results" method="POST">
Date: <input type="text" name="dated" size="10" value="01/12/2015">
Triggers: <input type="text" name="triggers" size="100" value="Trigger1|Trigger2"><br><br>
<textarea name ="text" rows="20" cols="150">random text here
</textarea>
<input autofocus type="submit" value="Submit">
</form>
</body>
</html>
请注意,您已经在表格中指定了method="POST"
,但是在服务器代码中已经指定了m.Get("/results",...)
。那条线应该是m.Post("/results",...)
。马提尼酒正在尝试路由请求,但没有POST /results
唯一的定义GET /results
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句