go项目添加跨域处理
1.在项目根目录下载依赖
go get github.com/gin-contrib/cors
2.在main.go合适位置添加
// 配置 CORS
r.Use(cors.New(cors.Config{
AllowOrigins: []string{"*"}, // 允许所有来源(生产环境建议改成前端域名如http://localhost:8080)
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowHeaders: []string{"Origin", "Content-Type", "Authorization"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
MaxAge: 12 * time.Hour,
}))
如

每天进步一点点

浙公网安备 33010602011771号