go的web框架gin实现jsonp跨域
Port:8080
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jsonp</title> <script type="text/javascript"> function sayHello(data){ alert(JSON.stringify(data)); } </script> <script type="text/javascript" src="http://localhost:8081/JSONP?callback=sayHello"></script> </head> <body> <h1>jsonp</h1> </body>
Port:8081
package main import ( "net/http" "github.com/gin-gonic/gin" ) func main() { r := gin.Default() r.Static("/assets", "./assets") r.GET("/JSONP", func(c *gin.Context) { data := map[string]interface{}{ "foo": "bar", } //callback is x // Will output : x({\"foo\":\"bar\"}) c.JSONP(http.StatusOK, data) }) // Listen and serve on 0.0.0.0:8080 r.Run(":8081") }