首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往
您找到你想要的搜索结果了吗?
是的
没有找到

go实现表单提交

package main import ( "fmt" "html/template" //支持模板html "log" //打印日志log类 "net/http" "strings" ) func sayHelloName(w http.ResponseWriter, r *http.Request) { r.ParseForm() fmt.Println(r.Form) fmt.Println("path", r.URL.Path) fmt.Println("scheme", r.URL.Scheme) fmt.Println(r.Form["url_long"]) for k, v := range r.Form { fmt.Println("key:", k) fmt.Println("val:", strings.Join(v, "")) } fmt.Fprintf(w, "hello box") } func login(w http.ResponseWriter, r *http.Request) { //打印请求的方法 fmt.Println("method", r.Method) if r.Method == "GET" { //如果请求方法为get显示login.html,并相应给前端 t, _ := template.ParseFiles("login.html") t.Execute(w, nil) } else { //否则走打印输出post接受的参数username和password fmt.Println(r.PostFormValue("username")) fmt.Println(r.PostFormValue("password")) } } func main() { //监听 / 走sayHelloName http.HandleFunc("/", sayHelloName) //路由控制/login 走login方法 http.HandleFunc("/login", login) err := http.ListenAndServe(":8081", nil) if err != nil { log.Fatal("ListenAndServe:", err) } } -------------------------------------------------- <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>go 登录test</title> </head> <body> <form method="POST" action="/login"> <input type="text" name="username" /> <input type="text" name="password" /> <input type="submit" value="登录"> </form> </body> </html>

02
领券