码迷,mamicode.com
首页 > 其他好文 > 详细

Go-gin CORS 跨域中间件

时间:2018-06-17 22:31:40      阅读:3235      评论:0      收藏:0      [点我收藏+]

标签:cache   control   stack   question   https   put   AC   ack   writer   

原文:https://stackoverflow.com/questions/29418478/go-gin-framework-cors

func CORSMiddleware() gin.HandlerFunc {
    return func(c *gin.Context) {
        c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
        c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
        c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
        c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")

        if c.Request.Method == "OPTIONS" {
            c.AbortWithStatus(204)
            return
        }

        c.Next()
    }
}

 

Go-gin CORS 跨域中间件

标签:cache   control   stack   question   https   put   AC   ack   writer   

原文地址:https://www.cnblogs.com/Bob-wei/p/9193820.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!