Returning JSON object in beego

huangapple go评论84阅读模式
英文:

Returning JSON object in beego

问题

我正在使用beego(golang框架),并尝试使用jquery ajax在go函数完成后更新我的网页。然而,我在返回JSON对象方面遇到了困难,以便jquery可以在其成功函数中处理它。在golang或beego中有没有办法返回JSON?谢谢。

英文:

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.

答案1

得分: 2

Beego控制器有一个ServeJson()函数,请查看这里

英文:

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

答案2

得分: 1

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

type Data struct {
//与您的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[&quot;JSON&quot;] = &amp;data
this.ServeJSON()

}

不要忘记导入 encoding/json

<!-- end snippet -->

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

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(&amp;data)
if err == nil {

    this.Data[&quot;JSON&quot;] = &amp;data
    this.ServeJSON()

}

Dont forget to import encoding/json

<!-- end snippet -->

huangapple
  • 本文由 发表于 2015年1月28日 00:56:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/28175800.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定