简体   繁体   English

通过 Angular 启用 CORS

[英]Enable CORS through Angular

I am using Angular 7, and i am supposed to construct a frontend, with the backend deployed on Heroku.我正在使用 Angular 7,我应该构建一个前端,后端部署在 Heroku 上。 Since i cannot edit the backend (which obviously does not have CORS enabled), i cannot access any api's and i get an error:由于我无法编辑后端(显然没有启用 CORS),我无法访问任何 api,并且出现错误:

Access to XMLHttpRequest at ' http://ebit-front-test.herokuapp.com/register ' from origin ' http://localhost:4200 ' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status. Access to XMLHttpRequest at ' http://ebit-front-test.herokuapp.com/register ' from origin ' http://localhost:4200 ' has been blocked by CORS policy: Response to preflight request doesn't pass access control check : 它没有 HTTP ok 状态。 register.service.ts:31 register.service.ts:31

Backend returned code 0, body was: [object ProgressEvent]后端返回代码 0,正文为:[object ProgressEvent]

Is there a way to enable CORS through Angular, or avoid this issue.有没有办法通过 Angular 启用 CORS,或者避免这个问题。 I have tried with custom proxy config but it was no good.我曾尝试使用自定义代理配置,但效果不佳。

*This question differs from the other similiar ones because they actually have access to their backend, whereas i have to solve the problem without access to backend. *这个问题与其他类似问题不同,因为他们实际上可以访问他们的后端,而我必须在没有访问后端的情况下解决问题。

You might want to setup local proxy in Angular.您可能想在 Angular 中设置本地代理。 In angular.json add option proxyConfig pointing to your definition file:angular.json添加选项proxyConfig指向您的定义文件:

        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "your-client:build",
            "proxyConfig": "proxy.conf.json"
          },

and create proxy.conf.json file for example:并创建proxy.conf.json文件,例如:

{
  "/register": {
    "target": "http://ebit-front-test.herokuapp.com/register",
    "secure": false
  }
}

and then from the client just call url as http.post('/register') .然后从客户端调用 url 作为http.post('/register') Tweak your proxy config so that it catches all api routes.调整您的代理配置,使其捕获所有 api 路由。

More reading... 更多阅读...

CORS needs to be configured on the server for a "real" application, one that you expect non-developers to use. CORS 需要在服务器上配置为“真正的”应用程序,您希望非开发人员使用该应用程序。

However, for testing purposes you can use a proxy server to add the headers that the sever should be adding.但是,出于测试目的,您可以使用代理服务器来添加服务器应添加的标头。

See for example, https://elements.heroku.com/buttons/marcus2vinicius/cors-anywhere例如,参见https://elements.heroku.com/buttons/marcus2vinicius/cors-anywhere

For everyone else having this issue i have found a way to avoid the CORS error.对于遇到此问题的其他所有人,我找到了一种避免 CORS 错误的方法。 If you run Chrome without web security, the CORS error will disappear.如果您在没有 web 安全性的情况下运行 Chrome,则 CORS 错误将消失。

This does not enable CORS, so it is a temporary solution, if you don't have access to your backend, but need to develop frontend.这不会启用 CORS,所以它是一个临时解决方案,如果您无法访问您的后端,但需要开发前端。

Steps to run without web security:在没有 web 安全性的情况下运行的步骤:

1) Run command prompt as administrator, and with this command kill all instances of Chrome: 1)以管理员身份运行命令提示符,并使用此命令杀死所有 Chrome 实例:

taskkill /F /IM chrome.exe taskkill /F /IM chrome.exe

2) Run new Chrome instance with web security disabled using this command: 2) 使用以下命令运行禁用 web 安全性的新 Chrome 实例:

start chrome https://teckangaroo.com/chrome-disable-web-security/ –disable-web-security –user-data-dir=”C:\teckangaroo.com”启动 chrome https://teckangaroo.com/chrome-disable-web-security/ –disable-web-security –user-data-dir=”C:\teckangaroo.com”

That should do it!应该这样做!

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

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