Go martini handling of HTML form data

huangapple go评论111阅读模式
英文:

Go martini handling of HTML form data

问题

我遇到了从HTML表单获取数据的问题。
模板显示在localhost:3000,我提交后被带到localhost:3000/results,显示"404页面未找到"的结果。URL中不包含任何表单字段。

以下是要翻译的代码部分:

  1. package main
  2. import (
  3. "html/template"
  4. "net/http"
  5. "github.com/go-martini/martini"
  6. )
  7. func main() {
  8. m := martini.Classic()
  9. m.Get("/", func(res http.ResponseWriter, req *http.Request) { // res and req are injected by Martini
  10. t, _ := template.ParseFiles("form.gtpl")
  11. t.Execute(res, nil)
  12. })
  13. m.Get("/results", func(r *http.Request) string {
  14. text := r.FormValue("text")
  15. return text
  16. })
  17. m.Run()
  18. }

模板内容如下:form.gtpl

  1. <html>
  2. <head>
  3. <title>Title Here</title>
  4. </head>
  5. <body bgcolor="#E6E6FA">
  6. <h1>Header Here</h1>
  7. <form action="/results" method="POST">
  8. Date: <input type="text" name="dated" size="10" value="01/12/2015">
  9. Triggers: <input type="text" name="triggers" size="100" value="Trigger1|Trigger2"><br><br>
  10. <textarea name ="text" rows="20" cols="150">random text here
  11. </textarea>
  12. <input autofocus type="submit" value="Submit">
  13. </form>
  14. </body>
  15. </html>
英文:

I am having trouble getting data from the HTML form.
The template shows at localhost:3000, I submit and am taken to localhost:3000/results with a "404 page not found" results. The URL does not include any of the forms fields.

  1. package main
  2. import (
  3. &quot;html/template&quot;
  4. &quot;net/http&quot;
  5. &quot;github.com/go-martini/martini&quot;
  6. )
  7. func main() {
  8. m := martini.Classic()
  9. m.Get(&quot;/&quot;, func(res http.ResponseWriter, req *http.Request) { // res and req are injected by Martini
  10. t, _ := template.ParseFiles(&quot;form.gtpl&quot;)
  11. t.Execute(res, nil)
  12. })
  13. m.Get(&quot;/results&quot;, func(r *http.Request) string {
  14. text := r.FormValue(&quot;text&quot;)
  15. return text
  16. })
  17. m.Run()
  18. }

and the template is: form.gtpl

  1. &lt;html&gt;
  2. &lt;head&gt;
  3. &lt;title&gt;Title Here&lt;/title&gt;
  4. &lt;/head&gt;
  5. &lt;body bgcolor=&quot;#E6E6FA&quot;&gt;
  6. &lt;h1&gt;Header Here&lt;/h1&gt;
  7. &lt;form action=&quot;/results&quot; method=&quot;POST&quot;&gt;
  8. Date: &lt;input type=&quot;text&quot; name=&quot;dated&quot; size=&quot;10&quot; value=&quot;01/12/2015&quot;&gt;
  9. Triggers: &lt;input type=&quot;text&quot; name=&quot;triggers&quot; size=&quot;100&quot; value=&quot;Trigger1|Trigger2&quot;&gt;&lt;br&gt;&lt;br&gt;
  10. &lt;textarea name =&quot;text&quot; rows=&quot;20&quot; cols=&quot;150&quot;&gt;random text here
  11. &lt;/textarea&gt;
  12. &lt;input autofocus type=&quot;submit&quot; value=&quot;Submit&quot;&gt;
  13. &lt;/form&gt;
  14. &lt;/body&gt;
  15. &lt;/html&gt;

答案1

得分: 4

在你的表单中,你指定了method="POST",但是在服务器代码中,你使用了m.Get("/results",...)。这一行应该改为m.Post("/results",...)。Martini试图路由请求,但是没有定义POST /results,只有GET /results

英文:

Notice in your form you've specified method=&quot;POST&quot;, but in the server code you have m.Get(&quot;/results&quot;,...). That line should be m.Post(&quot;/results&quot;,...). Martini is trying to route the request but there is no definition for POST /results only GET /results

答案2

得分: 0

将 m.GET 更改为 m.POST,然后问题解决了。

英文:

changed m.GET to m.POST, and voilà fixed.

huangapple
  • 本文由 发表于 2015年3月22日 02:27:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/29186413.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定