I am using beego (golang framework) and I am trying to use jquery ajax to update my web pages after the go function finishes. However I am stocked at returning JSON object so that the jquery could handle it in its success function. Is there any way to return JSON in golang or beego and how? Thanks.
Asked
Active
Viewed 2,314 times
0
-
1Check out the [`encoding/json`](http://golang.org/pkg/encoding/json/) package from the standard library. – icza Jan 27 '15 at 17:03
2 Answers
1
type Data struct {
//Values that equivalent to your ajax post
Data string
Id int
Name string
}
var data Data
req := this.Ctx.Request
dec := json.NewDecoder(req.Body)
err := dec.Decode(&data)
if err == nil {
this.Data["JSON"] = &data
this.ServeJSON()
}
Dont forget to import encoding/json

Patrick Mark Mazo
- 21
- 1