mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 12:50:55 +00:00
fix a bug about json and xml
This commit is contained in:
parent
45710c3a3b
commit
10e7dc856b
@ -272,14 +272,16 @@ Helper function for serving Json, sets content type to application/json:
|
|||||||
```go
|
```go
|
||||||
func (this *AddController) Get() {
|
func (this *AddController) Get() {
|
||||||
mystruct := { ... }
|
mystruct := { ... }
|
||||||
routes.ServeJson(w, &mystruct)
|
this.Data["json"] = &mystruct
|
||||||
|
this.ServeJson()
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
Helper function for serving Xml, sets content type to application/xml:
|
Helper function for serving Xml, sets content type to application/xml:
|
||||||
```go
|
```go
|
||||||
func (this *AddController) Get() {
|
func (this *AddController) Get() {
|
||||||
mystruct := { ... }
|
mystruct := { ... }
|
||||||
routes.ServeXml(w, &mystruct)
|
this.Data["xml"]=&mystruct
|
||||||
|
this.ServeXml()
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -127,7 +127,7 @@ func (c *Controller) Redirect(url string, code int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Controller) ServeJson() {
|
func (c *Controller) ServeJson() {
|
||||||
content, err := json.MarshalIndent(c.Data, "", " ")
|
content, err := json.MarshalIndent(c.Data["json"], "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(c.Ctx.ResponseWriter, err.Error(), http.StatusInternalServerError)
|
http.Error(c.Ctx.ResponseWriter, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@ -138,7 +138,7 @@ func (c *Controller) ServeJson() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Controller) ServeXml() {
|
func (c *Controller) ServeXml() {
|
||||||
content, err := xml.Marshal(c.Data)
|
content, err := xml.Marshal(c.Data["xml"])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(c.Ctx.ResponseWriter, err.Error(), http.StatusInternalServerError)
|
http.Error(c.Ctx.ResponseWriter, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user