0

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.

lkb
  • 11
  • 1
  • 1
    Check out the [`encoding/json`](http://golang.org/pkg/encoding/json/) package from the standard library. – icza Jan 27 '15 at 17:03

2 Answers2

2

Beego controller has a ServeJson() func, have a look at it.

yee
  • 1,975
  • 1
  • 15
  • 14
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