JSONP

Using JSONP to request data from a server in a different domain. Add callback to response body if the query parameter callback exists.

  1. func main() {
  2. r := gin.Default()
  3. r.GET("/JSONP?callback=x", func(c *gin.Context) {
  4. data := map[string]interface{}{
  5. "foo": "bar",
  6. }
  7. //callback is x
  8. // Will output : x({\"foo\":\"bar\"})
  9. c.JSONP(http.StatusOK, data)
  10. })
  11. // Listen and serve on 0.0.0.0:8080
  12. r.Run(":8080")
  13. }

Last modified March 7, 2020 : add blog dir (#115) (f46734b)