html 表单提交失败(404)通常是因为 http 方法不匹配:表单使用 `method=”post”`,但 martini 路由却用 `m.get(“/results”, …)` 定义,导致请求无法被正确路由。只需将 `get` 改为 `post` 并正确解析表单即可解决。
在 go 的 Martini 框架中,处理 html 表单数据的关键在于两点:HTTP 方法一致性 和 表单解析时机。你的原始代码中,
✅ 正确做法是使用 m.Post(“/results”, …) 注册处理函数,并确保在访问 r.FormValue() 前调用 r.ParseForm()(Martini 会自动完成此操作,但需确认中间件已启用;martini.Classic() 默认包含 martini.Logger、martini.Recovery 和 martini.Static,但不自动调用 ParseForm —— 实际上,r.FormValue 内部会惰性触发 ParseForm,所以通常可直接使用,但仍建议显式处理以提高健壮性)。
以下是修复后的完整示例:
package main import ( "html/template" "net/http" "github.com/go-martini/martini" ) func main() { m := martini.Classic() // 渲染首页表单(GET /) m.Get("/", func(res http.ResponseWriter, req *http.Request) { t, err := template.ParseFiles("form.gtpl") if err != nil { http.Error(res, "Template error: "+err.Error(), http.StatusInternalServerError) return } t.Execute(res, nil) }) // ✅ 关键修复:使用 Post() 而非 Get() m.Post("/results", func(res http.ResponseWriter, req *http.Request) string { // Martini 会自动解析表单(依赖 net/http 的默认行为),但建议显式调用(更可控) if err := req.ParseForm(); err != nil { return "Error parsing form: " + err.Error() } // 安全获取字段值(注意字段名与表单 name 属性严格一致) date := req.FormValue("dated") triggers := req.FormValue("triggers") text := req.FormValue("text") // 返回简单响应(生产环境建议渲染模板或返回 jsON) return "Received:nDate: " + date + "nTriggers: " + triggers + "nText: " + text }) m.Run() }
⚠️ 注意事项:
立即学习“前端免费学习笔记(深入)”;
总结:表单提交 404 的根本原因几乎总是路由方法不匹配。牢记「前端 method 决定后端路由动词」——POST 表单 → m.Post() 处理器,GET 表单 → m.Get(),并确保路径一致。
conntrack 表满导致新连接失败但 ss -s 显示正常连接数的排查路径
Composer怎么查看许可证信息 license命令列出协议【工具】