gin的控制器:
type ExampleService struct { UserName String Password String } func (example
*ExampleService) LoginCheck(c *gin.Context) bool { c.Bind(&example)
............ }前端应该写:this.axios.post("login.html", { UserName: this.username,
//这里对应struct的成员名 Password: this.password })
还有一种更轻量灵活的写法:
func LoginCheck(c *gin.Context) bool { var param struct { Username string
`json:"username" binding:"required"` Password string `json:"password"
binding:"required"` } c.Bind(¶m) ............................
前端照旧
划重点: 结构体成员变量名首字母必须大写!!!
补充:
当绑定的成员为int或者int32,int64类型时,前端如果传入的值为0,会报如下错误:
[GIN-debug] [WARNING] Headers were already written. Wanted to override status
code 400 with 200
且前端会报错误:Failed to load resource: the server responded with a status of 400
(Bad Request)
导致不正常.
解决办法1: 避免设计0值;
解决办法2: 去掉以下结构体的 binding:"required"
var json struct { Status int64 `json:"status" binding:"required"`
} c.Bind(&json)
热门工具 换一换