这是 astaxie 书中的简单形式,当我尝试 '/login' 时,我得到No Data received { Unable to load the webpage because the server sent no data. Error code: ERR_EMPTY_RESPONSE }这是代码:main.gopackage mainimport ( "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 回答
- 1 回答
- 0 关注
- 182 浏览
添加回答
举报
0/150
提交
取消