文章目录
- 前言
- 一、使用github.com/gin-contrib/cors
前言
今天遇到了前后端跨域问题,前后端跨域解决蛮简单的,下面是解决方案
一、使用github.com/gin-contrib/cors
go get github.com/gin-contrib/cors
在路由的地方
r := gin.Default()corsConfig := cors.DefaultConfig()corsConfig.AllowOrigins = []string{"*"}corsConfig.AllowMethods = []string{"GET", "POST", "PUT", "DELETE", "PATCH"}corsConfig.AllowHeaders = []string{"Origin", "Content-Length", "Content-Type", "Authorization"}r.Use(cors.New(corsConfig))
这样就解决了跨域问题,我听说还有其他的跨域问题,我暂时还没有遇到。