## gin
```
func Cors() gin.HandlerFunc {
return cors.New(cors.Config{
AllowOrigins: []string{"*"}, //[]string{"http://127.0.0.1:8080"}
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "PATCH"},
AllowHeaders: []string{"Origin", "Content-Length", "Content-Type", "token"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
// AllowOriginFunc: func(origin string) bool { //自定义过滤规则AllowOrigins会不起作用
// return origin == "http://127.0.0.1:8081"
// },
MaxAge: 12 * time.Hour,
})
}
```
`最开始我是AllowOrigins: []string{"*"}, 一直没有问题
后来我指定域名后AllowOrigins: []string{"http://www.xxx.com"}
前端请求接口报403
`
## vue
### ajax
```
const baseURL = 'http://127.0.0.1:8081/api'
const http = axios.create({
baseURL:baseURL,
timeout:60 * 1000
})
export default http
```
### vue.config.js
```
module.exports = {
devServer: {
open:true,
hotOnly:true,
port: 8081,
proxy: {
'/': {
target: 'http://127.0.0.1:3000',
changeOrigin: true,
secure:false,
ws: true,
pathRewrite: {
'^/': ''
}
}
}
}
}
```
![image.png](https://static.studygolang.com/220121/bd6232c6b865ac9cf29ecc7c00b5885a.png)
`实际请求接口是 127.0.0.1:3000/api/check `
#1