简体   繁体   English

CORS 问题 - preflightMissingAllowOriginHeader - Express Gateway 和 Axios

[英]CORS Problem - preflightMissingAllowOriginHeader - Express Gateway and Axios

I'm tyring to use Express Gateway + AXIOS (react) + Express, but I'm receiving the CORS Erro, I already did many thing but nothing worked.我很想使用 Express Gateway + AXIOS(反应)+ Express,但我收到了 CORS 错误,我已经做了很多事情但没有任何效果。

The error is this:错误是这样的: 在此处输入图像描述

Cross-Origin Resource Sharing error: PreflightMissingAllowOriginHeader

My EXPRESS is like this:我的EXPRESS是这样的:

 const corsOptions = { origin: '*', methods: ['POST', 'GET', 'PATCH', 'DELETE'], allowedHeaders: ['Content-Type', 'Authorization'] } app.use(cors(corsOptions));

My EXPRESS-GATEWAY :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

My AXIOS :我的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)

I don't know what to do anymore.我不知道该怎么办了。 Can someone help me?有人能帮我吗?

I already went in several posts but nothing worked..我已经进入了几个帖子,但没有任何效果..

edit: It didn't go to controller neither.编辑:go 到 controller 也没有。 edit2: I can use with POSTMAN, and it worked as expected...编辑2:我可以与 POSTMAN 一起使用,它按预期工作......

Add "OPTIONS" after "DELETE" in your lists of permitted methods in express/express-gateway.在 express/express-gateway 的允许方法列表中的“DELETE”之后添加“OPTIONS”。

 const corsOptions = { origin: '*', methods: ['POST', 'GET', 'PATCH', 'DELETE', 'OPTIONS'], allowedHeaders: ['Content-Type', 'Authorization'] } app.use(cors(corsOptions));

 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", "OPTIONS" ] 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM