从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>
型
2条答案
按热度按时间p1tboqfb1#
注意,在表单中指定了
method="POST"
,但在服务器代码中指定了m.Get("/results",...)
。这条线应该是m.Post("/results",...)
。Martini正在尝试路由请求,但没有POST /results
的定义,只有GET /results
x4shl7ld2#
将m.GET更改为m.POST,并修复。