fix a bug about json and xml
Showing
2 changed files
with
6 additions
and
4 deletions
| ... | @@ -272,14 +272,16 @@ Helper function for serving Json, sets content type to application/json: | ... | @@ -272,14 +272,16 @@ Helper function for serving Json, sets content type to application/json: |
| 272 | ```go | 272 | ```go |
| 273 | func (this *AddController) Get() { | 273 | func (this *AddController) Get() { |
| 274 | mystruct := { ... } | 274 | mystruct := { ... } |
| 275 | routes.ServeJson(w, &mystruct) | 275 | this.Data["json"] = &mystruct |
| 276 | this.ServeJson() | ||
| 276 | } | 277 | } |
| 277 | ``` | 278 | ``` |
| 278 | Helper function for serving Xml, sets content type to application/xml: | 279 | Helper function for serving Xml, sets content type to application/xml: |
| 279 | ```go | 280 | ```go |
| 280 | func (this *AddController) Get() { | 281 | func (this *AddController) Get() { |
| 281 | mystruct := { ... } | 282 | mystruct := { ... } |
| 282 | routes.ServeXml(w, &mystruct) | 283 | this.Data["xml"]=&mystruct |
| 284 | this.ServeXml() | ||
| 283 | } | 285 | } |
| 284 | ``` | 286 | ``` |
| 285 | 287 | ... | ... |
| ... | @@ -127,7 +127,7 @@ func (c *Controller) Redirect(url string, code int) { | ... | @@ -127,7 +127,7 @@ func (c *Controller) Redirect(url string, code int) { |
| 127 | } | 127 | } |
| 128 | 128 | ||
| 129 | func (c *Controller) ServeJson() { | 129 | func (c *Controller) ServeJson() { |
| 130 | content, err := json.MarshalIndent(c.Data, "", " ") | 130 | content, err := json.MarshalIndent(c.Data["json"], "", " ") |
| 131 | if err != nil { | 131 | if err != nil { |
| 132 | http.Error(c.Ctx.ResponseWriter, err.Error(), http.StatusInternalServerError) | 132 | http.Error(c.Ctx.ResponseWriter, err.Error(), http.StatusInternalServerError) |
| 133 | return | 133 | return |
| ... | @@ -138,7 +138,7 @@ func (c *Controller) ServeJson() { | ... | @@ -138,7 +138,7 @@ func (c *Controller) ServeJson() { |
| 138 | } | 138 | } |
| 139 | 139 | ||
| 140 | func (c *Controller) ServeXml() { | 140 | func (c *Controller) ServeXml() { |
| 141 | content, err := xml.Marshal(c.Data) | 141 | content, err := xml.Marshal(c.Data["xml"]) |
| 142 | if err != nil { | 142 | if err != nil { |
| 143 | http.Error(c.Ctx.ResponseWriter, err.Error(), http.StatusInternalServerError) | 143 | http.Error(c.Ctx.ResponseWriter, err.Error(), http.StatusInternalServerError) |
| 144 | return | 144 | return | ... | ... |
-
Please register or sign in to post a comment