18910140161

Golang解决跨域问题的Gin框架

顺晟科技

2021-08-28 09:40:09

64

首先,gin。创建一个返回HandlerFunc的函数

Func cors() gin。HandlerFunc {

Return func(c *gin)。Context) {

Method :=c.Request.Method

C.Header(' access-control-allow-origin ',' * ')

C.Header(‘access-control-allow-headers’、‘content-type、access token、x-csrf-toker’)

C.Header(' access-control-allow-methods ',' post,get,options ')

C.Header(‘access-control-expose-headers’、‘content-length、access-control-allow-))

C . header(' access-control-allow-credentials ',' true ')

If method==' OPTIONS ' {

C.abortwithstatus(http . status nocontent)

}

C.Next()

}

}

然后*gin .在可以获得Engine对象的地方调用以下方法即可

Engine。Use(cors())

相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航