我是gin-gonic框架的新手,我一直在尝试从html的get请求中添加的输入中读取值,但我无法读取我编写的值。
当我提交请求时,浏览器发送以下网址: http://localhost:3000/backend?name1=value1&name2=value2&name3=value3
我一直在寻找gin-gonic使用这种url类型的互联网,但我只发现它使用了这样的url http://localhost:3000/backend/value1
html代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form method="GET" action="/backend">
<input id="store" name="name1" type="text">
<input id="razon" name="name2" type="text">
<input id="total" name="name3" type="text">
<input type="submit" value="submit">
</form>
</body>
</html>
golang代码:
package main
import(
"net/http"
"fmt"
"github.com/gin-gonic/contrib/static"
"github.com/gin-gonic/gin"
)
func main(){
router := gin.Default()
router.Use(static.Serve("/",static.LocalFile("./views",true)))
router.GET("/backend",func(c *gin.Context){
fmt.Println(c.Param("name1"))
c.JSON(http.StatusOK, gin.H{
"name1" : c.Param("name1"),
})
})
router.Run(":3000")
}
实际结果: {“ name1”:“”}
预期结果: {“ name1”:“ value1”}
答案 0 :(得分:0)
您要查找的功能不是Param()
,而是Query()
。