【问题标题】:Go handling of HTML form data去处理 HTML 表单数据
【发布时间】:2015-03-21 18:27:22
【问题描述】:

我无法从 HTML 表单获取数据。 模板显示在 localhost:3000,我提交并被带到 localhost:3000/results,结果显示“404 page not found”。 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>

【问题讨论】:

    标签: go forms martini http-post


    【解决方案1】:

    请注意,您在表单中指定了method="POST",但在服务器代码中您指定了m.Get("/results",...)。那行应该是m.Post("/results",...)。 Martini 正在尝试路由请求,但没有定义 POST /results 只有 GET /results

    【讨论】:

      【解决方案2】:

      将 m.GET 更改为 m.POST,并且修复了。

      【讨论】:

        猜你喜欢
        • 2013-01-29
        • 1970-01-01
        • 2016-06-14
        • 2016-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多