Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add JSONP result type and method to controller

  • Loading branch information...
commit 912ee0b3ed0c9e14ec0c5a7698d196dd9d65ad99 1 parent a4d794c
@andrewmunsell andrewmunsell authored
Showing with 31 additions and 0 deletions.
  1. +5 −0 controller.go
  2. +26 −0 results.go
View
5 controller.go
@@ -119,6 +119,11 @@ func (c *Controller) RenderJson(o interface{}) Result {
return RenderJsonResult{o}
}
+// Renders a JSONP result using encoding/json.Marshal
+func (c *Controller) RenderJsonP(callback string, o interface{}) Result {
+ return RenderJsonPResult{callback, o}
+}
+
// Uses encoding/xml.Marshal to return XML to the client.
func (c *Controller) RenderXml(o interface{}) Result {
return RenderXmlResult{o}
View
26 results.go
@@ -211,6 +211,32 @@ func (r RenderJsonResult) Apply(req *Request, resp *Response) {
resp.Out.Write(b)
}
+type RenderJsonPResult struct {
+ callback string
+ obj interface{}
+}
+
+func (r RenderJsonPResult) Apply(req *Request, resp *Response) {
+ var b []byte
+ var err error
+ if Config.BoolDefault("results.pretty", false) {
+ b, err = json.MarshalIndent(r.obj, "", " ")
+ } else {
+ b, err = json.Marshal(r.obj)
+ }
+
+ if err != nil {
+ ErrorResult{Error: err}.Apply(req, resp)
+ return
+ }
+
+ var callback string
+ callback = r.callback + "(" + string(b[:]) + ");"
+
+ resp.WriteHeader(http.StatusOK, "application/javascript")
+ resp.Out.Write([]byte(callback))
+}
+
type RenderXmlResult struct {
obj interface{}
}
Please sign in to comment.
Something went wrong with that request. Please try again.