https://api.paperai.life/chatapi (需要先在主页登录)
这里点击create token往我的one API请求的时候会被跨域拦截
报错:
Access to fetch at 'https://chatapi.paperai.life/api/token' from
origin 'https://api.paperai.life' has been blocked by CORS policy:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
If an opaque response serves your needs,
set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
page-c1af2c27e951ea17.js:1
但是如果直接请求根路径的话是不会拦截的,
对子路径的get请求没有拦截
而且我已经做了很多设置仍然不行
跨域配置
func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
origin := c.GetHeader("Origin")
// 设置允许的 Origin
c.Writer.Header().Set("Access-Control-Allow-Origin", origin)
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT, DELETE")
c.Writer.Header().Set("Vary", "Origin")
c.Writer.Header().Set("Access-Control-Expose-Headers", "Access-Control-Allow-Origin, Access-Control-Allow-Credentials")
c.Writer.Header().Set("Access-Control-Max-Age", "6000")
// 记录日志
common.SysLog("CORS allowed for origin:" + origin)
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(http.StatusNoContent)
return
}
c.Next()
}
}