Commit 5d0f254f by 彭芳

minor: add cors

parent e1b7b845
...@@ -28,3 +28,10 @@ func (p *Common) PassMethods(c *gin.Context) { ...@@ -28,3 +28,10 @@ func (p *Common) PassMethods(c *gin.Context) {
return return
} }
} }
// Cors 允许服务器跨域访问
func (p *Common) Cors(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Methods", "OPTIONS,HEAD,GET,POST,DELETE,PUT,PATCH")
c.Header("Access-Control-Allow-Headers", "Authorization,Content-Type,Content-Range,Content-Disposition,token,access-token,device,x-requested-with")
}
...@@ -31,7 +31,7 @@ type Web struct { ...@@ -31,7 +31,7 @@ type Web struct {
// @in header // @in header
// @name token // @name token
func (p *Web) RouterInit(engine *gin.Engine) { func (p *Web) RouterInit(engine *gin.Engine) {
engine.Use(p.Common.SetRequestId, p.Common.PassMethods) engine.Use(p.Common.SetRequestId, p.Common.Cors, p.Common.PassMethods)
engine.NoRoute(func(c *gin.Context) { engine.NoRoute(func(c *gin.Context) {
c.JSON(http.StatusNotFound, gin.H{"code": "404", "message": "Page not found"}) c.JSON(http.StatusNotFound, gin.H{"code": "404", "message": "Page not found"})
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment