Gin跨域问题
2022-07-31 本文已影响0人
会爬虫的小蟒蛇
Gin的跨域比较简单 用轮子就好
go get -u github.com/gin-contrib/cors
使用默认的跨域策略
r := gin.Default()
r.Use(cors.Default())
// 此处cors.Default 已经将基础跨域的信息配置好了
自定义跨域策略
import (
"time"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
func Cors() gin.HandlerFunc {
c := cors.Config{
AllowAllOrigins: true,
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "PATCH"},
AllowHeaders: []string{"Content-Type", "Access-Token", "Authorization"},
MaxAge: 6 * time.Hour,
}
return cors.New(c)
}