這是 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 回答
- 0 關注
- 198 瀏覽
添加回答
舉報
0/150
提交
取消