前端代码
```
$("#btn").click(function(){
$.post({
url: 'http://localhost:9999/class_add',
dataType:"json",
data: {
class_name:"hulu",
class_id:"55"
},
success: function (res) {
console.log(">>>>", res)
}
})
})
```
后端代码
```
func (c *BlogController) Add() {
c.Ctx.ResponseWriter.Header().Add("Access-Control-Allow-Origin", "*")
c.Ctx.ResponseWriter.Header().Add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
class := models.Class{}
spew.Dump("↓↓↓↓↓↓↓↓↓↓↓↓↓↓ c.Ctx.Input.RequestBody ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓", c.Ctx.Input.RequestBody)
json.Unmarshal(c.Ctx.Input.RequestBody, &class)
var err error
if err = json.Unmarshal(c.Ctx.Input.RequestBody, &class); err == nil {
c.Data["json"] = &class
} else {
fmt.Println(err)
c.Data["json"] = err
}
c.ServeJSON()
}
```
解析结果
{Offset: 1}
报第一个字母解析错误,
有疑问加站长微信联系(非本文作者))