gin_cors/README.md
2022-01-04 10:33:54 +08:00

39 lines
859 B
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#gin_cors
##说明
gin框架的cors跨域处理中间件
##使用
go get github.com/cowardmrx/gin_cors
```go
r := gin.Default()
// 创建cors配置 只要是string类型的一律按照 , 分割只有AccessControlAllowCredentials是字符串类型的bool值
cors := &Cors{
// 允许的域名
AccessControlAllowOrigins: []string{
"http://localhost",
},
// 允许的请求头 使用 ',' 分割
AccessControlAllowHeaders: "Authorization",
// 允许的请求方式 使用 ',' 分割
AccessControlAllowMethods: "PUT,POST,GET,DELETE",
// 允许暴露的请求头 使用 ',' 分割
AccessControlExposeHeaders: "Authorization",
// 允许凭证记录 true | false
AccessControlAllowCredentials: 'true'
}
r.POST("/te", func(c *gin.Context) {
c.JSON(200, gin.H{
"data": "this is data",
})
})
r.Run(":7878")
}
```