doujiushi9007 2015-02-19 15:20
浏览 55
已采纳

获取没有收到的Go语言表格数据

It's the simple form from astaxie's book when I try '/login' , I get

No Data received { Unable to load the webpage because the server sent no data.   Error code: ERR_EMPTY_RESPONSE }

Here's the code:

main.go

package main
import (
    "fmt"
    "html/template"
    "net/http"

)

func sayhelloName(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello astaxie!") // write data to response
}

func login(w http.ResponseWriter, r *http.Request) {
    fmt.Println("method:", r.Method) //get request method
    if r.Method == "GET" {
      t, err :=template.New("").Parse(loginHtml)
      if err != nil {
          panic(err)
      }

      const loginHtml = `
      <html>
      <head>
      <title></title>
      </head>
      <body>
      <form action="/login" method="post">
          Username:<input type="text" name="username">
          Password:<input type="password" name="password">
          <input type="submit" value="Login">
      </form>
      </body>
      </html>
      `

    }    else {
        r.ParseForm()
        // logic part of log in
        fmt.Println("username:", r.PostFormValue("username"))
        fmt.Println("password:", r.PostFormValue("password"))
    }
}


func main() {
    http.HandleFunc("/", sayhelloName) // setting router rule
    http.HandleFunc("/login", login)
   http.ListenAndServe(":9090", nil) // setting listening port

}

#login.gtpl
<html>
<head>
<title></title>
</head>
<body>
<form action="/login" method="post">
    Username:<input type="text" name="username">
    Password:<input type="password" name="password">
    <input type="submit" value="Login">
</form>
</body>
</html>

Any idea??
  • 写回答

1条回答 默认 最新

  • doupian9798 2015-02-19 15:23
    关注

    The problem with your original question (which has been edited several times) was that your ParseFiles() function failed, it was not able to read your template file. You didn't know about this because the error it returned you just discarded. Never do that! The least you can do is print the error or call panic(err) if it occurs. Would you have done that, you would have seen the cause immediately.

    The login.gtpl file has to be placed in the working directory you start you app from if you specify a relative path. Or specify an absolute path.

    You can also put your HTML source into your Go file like this until you sort things out:

    t, err := template.New("").Parse(loginHtml)
    if err != nil {
        panic(err)
    }
    t.Execute(w, nil)
    // ... the rest of your code
    
    
    // At the end of your .go source file (outside of login() func) insert:
    const loginHtml = `
    <html>
    <head>
    <title></title>
    </head>
    <body>
    <form action="/login" method="post">
        Username:<input type="text" name="username">
        Password:<input type="password" name="password">
        <input type="submit" value="Login">
    </form>
    </body>
    </html>
    `
    

    Note #1:

    Since your HTML template is just a static HTML, in its current form you can simply just send it to the output without building and executing a template from it:

    // Instead of calling template.New(...).Parse(...) and t.Execute(...), just do:
    w.Write([]byte(loginHtml))
    

    Note #2:

    The Request.Form is only available after Request.ParseForm() has been called, so do that prior to accessing it. Also for POST forms you might want to use Request.PostForm instead.

    As an alternative you can use the Request.PostFormValue() method which does this for you automatically if it has not yet been called:

    fmt.Println("username:", r.PostFormValue("username"))
    fmt.Println("password:", r.PostFormValue("password"))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题