简体   繁体   English

CORS 标头问题 express Angular

[英]CORS headers issue express Angular

In dev this works fine, i recently update the code in my vps and I'm getting an error in console: Acces to XMLHttpRequest at 'https://www.example.con/api/register' from origin 'https://example.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.在开发中这工作正常,我最近更新了我的 vps 中的代码,我在控制台中收到一个错误:访问 XMLHttpRequest at 'https://www.example.con/api/register' from origin 'https:// example.com' 已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头。 I tryed with :我试过:

 app.use((req, res, next) => { res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Headers', 'Authorization, X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Allow-Request-Method'); res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, DELETE'); res.header('Allow', 'GET, POST, OPTIONS, PUT, DELETE'); next(); });
and: app.use(cors()); 和: app.use(cors()); but i'm still having the same message in console. 但我仍然在控制台中收到相同的消息。
 noAuthHeader = { headers: new HttpHeaders({ 'NoAuth': 'True' }) }; postUser(user: User){ return this.http.post(this.url + '/register', user, this.noAuthHeader); }

 register: (req, res, next) => { let user = new User(); user.email = req.body.email; user.password = req.body.password; user.domain = req.body.domain; user.save((err, doc) => { if (!err) res.send(doc); else { if (err.code == 11000) { if(err.keyValue.hasOwnProperty('domain')){ res.status(422).send(['Name repeated']); } if(err.keyValue.hasOwnProperty('email')){ res.status(422).send(['Email repeated']); } } else { return next(err); } } }); },

The modifications i've been made in code is the verification in register() api.我在代码中所做的修改是 register() api 中的验证。 I'm using mongo DB and checking error code 11000 for repeated unique model data in DB (email & domain fields).我正在使用 mongo DB 并检查错误代码 11000 以查找 DB 中重复的唯一模型数据(电子邮件和域字段)。 Console throws CORS error when i try to register a duplicate email or domain.当我尝试注册重复的电子邮件或域时,控制台会引发 CORS 错误。

EDITED: I'm getting this error in console:编辑:我在控制台中收到此错误:

/home/myapp_full/myapp_v0.2/node_modules/mongoose/lib/helpers/promiseOrCallback.js:19 throw error; /home/myapp_full/myapp_v0.2/node_modules/mongoose/lib/helpers/promiseOrCallback.js:19 抛出错误; ^ TypeError: Cannot read property 'hasOwnProperty' of undefined. ^ 类型错误:无法读取未定义的属性“hasOwnProperty”。

I don't think you should need CORS unless you actually intend to have two separate websites: www.example.com and example.com .我不认为您应该需要 CORS,除非您确实打算拥有两个单独的网站: www.example.comexample.com

If, instead, your server is set up to accept requests from both, and doesn't redirect, I'd recommend that you handle redirects in your code wherever possible, so that you select one canonical URL: Either example.com or www.example.com .相反,如果您的服务器设置为接受来自两者的请求,并且不重定向,我建议您尽可能在代码中处理重定向,以便您选择一个规范 URL: example.comwww.example.com Barring that, as long as the script exists on both servers, just load the script without the URL (aka "/register" , not this.url + "/register" )除此之外,只要脚本存在于两个服务器上,只需加载没有 URL 的脚本(又名"/register" ,而不是this.url + "/register"

One time i had the same issue with Angular and i resolved it deleting the CORS headers in the back-end.有一次我在使用 Angular 时遇到了同样的问题,我通过删除后端中的 CORS 标头解决了这个问题。 I hope that works for you.我希望这对你有用。

Solved: It seems that Linux and windows throws different error messages, i modified this piece of code and it's working fine, anyways the response is just 'email or name repeated' and i would like to handle each unique data, i'm still waiting for an upgrade, thanks for your time.已解决:Linux 和 Windows 似乎抛出了不同的错误消息,我修改了这段代码并且它工作正常,无论如何响应只是“电子邮件或名称重复”,我想处理每个唯一的数据,我仍在等待升级,感谢您的时间。

 user.save((err, doc) => { if (!err) res.send(doc); else { if (err.code == 11000) { res.status(422).send(['Email or name repeated']); } else { return next(err); } } });

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

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