顺晟科技
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())
08
1972-02
21
2022-04
05
2022-03
29
2021-08
29
2021-08
29
2021-08