diff --git a/.gitignore b/.gitignore index 6cb9d573..afeee706 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ # Website -public -.publish +website/public # Node.js node_modules diff --git a/recipes/jsonp/public/index.html b/recipes/jsonp/public/index.html new file mode 100644 index 00000000..033632e9 --- /dev/null +++ b/recipes/jsonp/public/index.html @@ -0,0 +1,36 @@ + + + + + + + JSONP + + + + + + +
+ +

+


+        

+
+ + + diff --git a/recipes/jsonp/server.go b/recipes/jsonp/server.go new file mode 100644 index 00000000..8d74d480 --- /dev/null +++ b/recipes/jsonp/server.go @@ -0,0 +1,31 @@ +package main + +import ( + "math/rand" + "net/http" + "time" + + "github.com/labstack/echo" +) + +func main() { + // Setup + e := echo.New() + e.ServeDir("/", "public") + + e.Get("/jsonp", func(c *echo.Context) error { + callback := c.Query("callback") + var content struct { + Response string `json:"response"` + Timestamp time.Time `json:"timestamp"` + Random int `json:"random"` + } + content.Response = "Sent via JSONP" + content.Timestamp = time.Now().UTC() + content.Random = rand.Intn(1000) + return c.JSONP(http.StatusOK, callback, &content) + }) + + // Start server + e.Run(":3999") +} diff --git a/website/content/recipes/jsonp.md b/website/content/recipes/jsonp.md new file mode 100644 index 00000000..119d254c --- /dev/null +++ b/website/content/recipes/jsonp.md @@ -0,0 +1,95 @@ +--- +title: JSONP +menu: + main: + parent: recipes +--- + +JSONP is a method that allows cross-domain server calls. You can read more about it at the JSON versus JSONP Tutorial. + +## Server + +`server.go` + +```go +package main + +import ( + "math/rand" + "net/http" + "time" + + "github.com/labstack/echo" +) + +func main() { + // Setup + e := echo.New() + e.ServeDir("/", "public") + + e.Get("/jsonp", func(c *echo.Context) error { + callback := c.Query("callback") + var content struct { + Response string `json:"response"` + Timestamp time.Time `json:"timestamp"` + Random int `json:"random"` + } + content.Response = "Sent via JSONP" + content.Timestamp = time.Now().UTC() + content.Random = rand.Intn(1000) + return c.JSONP(http.StatusOK, callback, &content) + }) + + // Start server + e.Run(":3999") +} +``` + +## Client + +`index.html` + +```html + + + + + + + JSONP + + + + + + +
+ +

+


+        

+
+ + + +``` + +## Maintainers + +- [willf](http://github.com/willf) + +## [Source Code](https://github.com/labstack/echo/blob/master/recipes/jsonp)