Access to XMLHttpRequest at 'http://localhost:1111/…' from origin 'http://localhost:4200' has been blocked by CORS policy:
由于CORS策略的阻止,没有发生跨来源。
我在Windows系统上。
在
1 2 3 4 5 6 7 8 9 10 11 | makeHeaders() { const headers = new HttpHeaders({ 'content':"application/json", 'Content-Type': 'application/x-www-form-urlencoded', // 'Access-Control-Allow-Credentials': 'true', 'Access-Control-Allow-Origin': 'http://localhost:4200', 'Access-Control-Allow-Methods': 'OPTIONS, GET, POST', 'Access-Control-Allow-Headers': 'Origin, Content-Type, Accept, Access-Control-Allow-Origin, Authorization, X-Requested-With' }) return headers; } |
:
1 | "target":"http://localhost:1111" |
我在浏览器控制台中遇到此错误:
Access to XMLHttpRequest at 'http://localhost:1111/api/v1/employees/create' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
core.js:15724 ERROR HttpErrorResponse{headers: HttpHeaders, status: 0, statusText:"Unknown Error", url:"http://localhost:1111/api/v1/employees/create", ok: false,…}
谢谢
您可以尝试这个
1 2 3 4 5 6 7 8 9 10 11 | app.options("/*", function(req, res, next) { res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS'); res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With'); res.sendStatus(200); }); app.all('*', function(req, res, next) { res.header("Access-Control-Allow-Origin","*"); next(); }); |
在localhost上工作时,需要安装插件。
Chrome
https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=zh-CN
FireFox
https://addons.mozilla.org/zh-CN/firefox/addon/cors-everywhere/
谢谢