go項目添加跨域處理
1.在項目根目錄下載依賴
go get github.com/gin-contrib/cors
2.在main.go合適位置添加
// 配置 CORS
r.Use(cors.New(cors.Config{
AllowOrigins: []string{"*"}, // 允許所有來源(生產(chǎn)環(huán)境建議改成前端域名如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,
}))
如

每天進步一點點

浙公網(wǎng)安備 33010602011771號