Merge pull request #68 from luw2007/luw2007
feture: add jsonp
Showing
1 changed file
with
17 additions
and
0 deletions
| ... | @@ -216,6 +216,23 @@ func (c *Controller) ServeJson() { | ... | @@ -216,6 +216,23 @@ func (c *Controller) ServeJson() { |
| 216 | c.Ctx.ResponseWriter.Write(content) | 216 | c.Ctx.ResponseWriter.Write(content) |
| 217 | } | 217 | } |
| 218 | 218 | ||
| 219 | func (c *Controller) ServeJsonp() { | ||
| 220 | content, err := json.MarshalIndent(c.Data["jsonp"], "", " ") | ||
| 221 | if err != nil { | ||
| 222 | http.Error(c.Ctx.ResponseWriter, err.Error(), http.StatusInternalServerError) | ||
| 223 | return | ||
| 224 | } | ||
| 225 | callback := c.Ctx.Request.Form.Get("callback") | ||
| 226 | if callback == "" { | ||
| 227 | http.Error(c.Ctx.ResponseWriter, `"callback" parameter required`, http.StatusInternalServerError) | ||
| 228 | return | ||
| 229 | } | ||
| 230 | callback_content := fmt.Sprintf("%s(%s);\r\n", callback, string(content)) | ||
| 231 | c.Ctx.SetHeader("Content-Length", strconv.Itoa(len(callback_content)), true) | ||
| 232 | c.Ctx.ResponseWriter.Header().Set("Content-Type", "application/jsonp;charset=UTF-8") | ||
| 233 | c.Ctx.ResponseWriter.Write([]byte(callback_content)) | ||
| 234 | } | ||
| 235 | |||
| 219 | func (c *Controller) ServeXml() { | 236 | func (c *Controller) ServeXml() { |
| 220 | content, err := xml.Marshal(c.Data["xml"]) | 237 | content, err := xml.Marshal(c.Data["xml"]) |
| 221 | if err != nil { | 238 | if err != nil { | ... | ... |
-
Please register or sign in to post a comment