From 687fffead3d7cc1d874fde1e12458d27e549a1fd Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Fri, 15 Sep 2023 12:48:18 +0800 Subject: [PATCH] fix cors --- cors.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/cors.go b/cors.go index ec5e2fd..ddae754 100644 --- a/cors.go +++ b/cors.go @@ -6,13 +6,26 @@ import ( // Middleware function to handle CORS requests func handleCORS(c *gin.Context) { - c.Writer.Header().Set("Access-Control-Allow-Origin", "*") - c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, PATCH") - c.Writer.Header().Set("Access-Control-Allow-Headers", "Origin, Authorization, Content-Type") - if c.Request.Method == "OPTIONS" { + header := c.Writer.Header() + header.Set("Access-Control-Allow-Origin", "*") + header.Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, PATCH") + header.Set("Access-Control-Allow-Headers", "Origin, Authorization, Content-Type") + c.AbortWithStatus(200) return } + c.Next() + + header := c.Writer.Header() + + header.Del("Access-Control-Allow-Origin") + header.Del("Access-Control-Allow-Methods") + header.Del("Access-Control-Allow-Headers") + + header.Set("Access-Control-Allow-Origin", "*") + header.Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, PATCH") + header.Set("Access-Control-Allow-Headers", "Origin, Authorization, Content-Type") + }