You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
25 lines
1.4 KiB
25 lines
1.4 KiB
1 year ago
|
package utils
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
|
||
|
"github.com/gin-gonic/gin"
|
||
|
)
|
||
|
|
||
|
func CorsHandler() gin.HandlerFunc {
|
||
|
return func(context *gin.Context) {
|
||
|
context.Writer.Header().Set("Access-Control-Allow-Origin", "*")
|
||
|
context.Header("Access-Control-Allow-Methods", "*")
|
||
|
context.Header("Access-Control-Allow-Headers", "*")
|
||
|
context.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers,Cache-Control,Content-Language,Content-Type,Expires,Last-Modified,Pragma,FooBar") // 跨域关键设置 让浏览器可以解析
|
||
|
context.Header("Access-Control-Max-Age", "172800") // 缓存请求信息 单位为秒
|
||
|
context.Header("Access-Control-Allow-Credentials", "false") // 跨域请求是否需要带cookie信息 默认设置为true
|
||
|
context.Header("content-type", "application/json") // 设置返回格式是json
|
||
|
//Release all option pre-requests
|
||
|
if context.Request.Method == http.MethodOptions {
|
||
|
context.JSON(http.StatusOK, "Options Request!")
|
||
|
}
|
||
|
context.Next()
|
||
|
}
|
||
|
}
|