gin的控制器:
type ExampleService struct { UserName String Password String } func (example
*ExampleService) LoginCheck(c *gin.Context) bool { c.Bind(&example)
............ }前端應(yīng)該寫:this.axios.post("login.html", { UserName: this.username,
//這里對應(yīng)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) ............................
前端照舊
劃重點(diǎn): 結(jié)構(gòu)體成員變量名首字母必須大寫!!!
補(bǔ)充:
當(dāng)綁定的成員為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)
導(dǎo)致不正常.
解決辦法1: 避免設(shè)計(jì)0值;
解決辦法2: 去掉以下結(jié)構(gòu)體的 binding:"required"
var json struct { ????????Status int64 `json:"status" binding:"required"`
????} ????c.Bind(&json)
熱門工具 換一換