Home > Software engineering >  How solve Acces has been blocked by CORS policy?
How solve Acces has been blocked by CORS policy?

Time:09-09

hello I have this common error, and I don't understand how to remove it, useful in front of Angular14 and I try to call an https URL which works if I enter it in the navigation bar.

i try to use anuglar proxy but not working

I know that I can use a browser plugin to disable it, I would like none of the users to install it.

And I would like to know if it is possible and how to simulate another orgins because I work in localhost for the front and my back and on a server.

Access to XMLHttpRequest at '<my https url back' from origin 'localhost' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

proxy-prod.conf.json/proxy-dev.conf.json

    "/api": {
      "target": ["http://localhost:8080","https://myurl.com",...],
      "secure": false
    }

angular.json

,
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "configurations": {
            "production": {
              "browserTarget": "myapp:build:production",
              "proxyConfig": "src/proxy-prod.conf.json"
            },
            "development": {
              "browserTarget": "myapp:build:development",
              "proxyConfig": "src/proxy.conf.json"
            }
          },
          "defaultConfiguration": "development"
        },

CodePudding user response:

Is server side err, they need to accept your host url in the cors polocy.

CodePudding user response:

The CORS mechanism is implemented by the browser. It means that if you bypass the browser (with a proxy server, for instance) you will be able to bypass this validation.

In angular application you can use the proxy on development server.

Create a file name proxy.conf.json with following content:

{
    "/": {
      "target": "http://localhost:5000",
      "secure": false
    }
}

Note: replace for your hostname

Then, run the angular application with following command:

ng serve --proxy-config=proxy.conf.json

See this for reference

  • Related