06e002cf by lw

feture: add jsonp

1 parent 73f0f4f3
...@@ -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 {
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!