【发布时间】:2021-04-29 22:14:27
【问题描述】:
我很想使用 Express Gateway + AXIOS (react) + Express,但我收到了 CORS Erro,我已经做了很多事情但没有任何效果。
Cross-Origin Resource Sharing error: PreflightMissingAllowOriginHeader
我的EXPRESS是这样的:
const corsOptions = {
origin: '*',
methods: ['POST', 'GET', 'PATCH', 'DELETE'],
allowedHeaders: ['Content-Type', 'Authorization']
}
app.use(cors(corsOptions));
我的EXPRESS-GATEWAY:
http:
port: 8080
admin:
port: 9876
host: localhost
apiEndpoints:
api:
host: "localhost"
paths:
- '/api/B/*'
- '/api/A/*'
serviceEndpoints:
appname:
urls:
- 'http://localhost:8000'
policies:
- jwt
- cors
- expression
- log
- proxy
- rate-limit
pipelines:
default:
apiEndpoints:
- api
policies:
- cors:
- action:
origin: ["*"]
methods: [ "HEAD", "PUT", "PATCH", "POST", "GET", "DELETE" ]
credentials: true
allowedHeaders: ['Content-type','Authorization','Origin','Access-Control-Allow-Origin','Accept','Options','X-Requested-With']
- jwt:
- action:
secretOrPublicKey: code
checkCredentialExistence: false
- proxy:
- action:
serviceEndpoint: appname
changeOrigin: true
我的AXIOS:
const headers = {
headers: {
"Authorization": authToken,
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true
},
}
axios.get(`${API_PRIVATE_URL}/user/profile`, {
crossdomain: true
}, {
withCredentials: true
}, headers)
我不知道该怎么办了。有人可以帮我吗?
我已经发了几个帖子,但没有任何效果..
编辑:它也没有进入控制器。 edit2:我可以与 POSTMAN 一起使用,它按预期工作......
【问题讨论】:
-
为什么需要在 Express 服务和 Express Gateway 上实施 CORS?
-
@JamesMcLeod 我不需要...我只是这样做看看它是否有效..
-
不确定这是否是同一个问题,但我遇到了一个非常相似的问题,结果证明我的代理不允许对我尝试访问的 API 的 OPTIONS 请求。允许 OPTIONS 为我修复了它。
-
我同意 adamgy 的观点,我在未能提供 OPTIONS http 方法时看到过这样的失败。
-
如果它使用 Postman 工作,我建议记录来自 Postman 的完整请求,包括标头,并分析 Postman 添加的内容(特别是在标头中)与您发出请求的方式不同。
标签: express axios express-gateway