lkb
lkb

Reputation: 11

Returning JSON object in beego

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.

Upvotes: 0

Views: 2389

Answers (2)

Patrick Mark Mazo
Patrick Mark Mazo

Reputation: 21

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

Upvotes: 1

yee
yee

Reputation: 1985

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

Upvotes: 2

Related Questions