| 3 | import "github.com/gin-gonic/gin" |
| 4 | |
| 5 | func Cors(c *gin.Context) { |
| 6 | c.Writer.Header().Set("Access-Control-Allow-Origin", "*") |
| 7 | c.Writer.Header().Set("Access-Control-Allow-Origin", "*") |
| 8 | c.Writer.Header().Set("Access-Control-Allow-Credentials", "true") |
| 9 | c.Writer.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type, Accept") |
| 10 | c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS") |
| 11 | // 如果是OPTIONS请求,直接返回 |
| 12 | if c.Request.Method == "OPTIONS" { |
| 13 | c.AbortWithStatus(204) |
| 14 | return |
| 15 | } |
| 16 | c.Next() |
| 17 | } |