Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify 'Access-Control-Allow-Origin' when running angular-cli serve

Tags:

angular-cli

I'm running a Spring API server and an Angular-cli server to serve up my static content. In production we will be using a CDN, but for development both the front and backend servers are running on my local box on different ports. The Spring server serves up the initial html page and then the rest of the JS, CSS, and html come from the angular-cli/CDN.

The problem is that when the call to System.import() is made, the browser complains about CORS: XMLHttpRequest cannot load http://localhost:4200/system-config.js. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. zone.js:323 Error: Error: XHR error loading http://localhost:4200/system-config.js(…)

How do I configure angular-cli to set the 'Access-Control-Allow-Origin' header so the browser won't puke.

like image 563
wholladay Avatar asked May 17 '16 17:05

wholladay


People also ask

How do I enable CORS in angular 10?

Enable CORS with Proxy Configuration Settings in Angular. To enable CORS via proxy configuration, we need to generate a src/proxy. conf. json file inside the Angular root folder and also place the following code inside of it. We used the secure property to enable the deliberate use of SSL.

How do you fix the CORS issue in angular 11?

CORS error due to browser's same origin policy. To get around this, you need to tell your browser to enable your client and your server to share resources while being of different origins. In other words, you need to enable cross-origin resource sharing or CORS in your application.

What is CORS in angular?

CORS is an HTTP header- based mechanism that allows the server to indicate any other domain, scheme, or port origins and enables them to be loaded.


2 Answers

FWIW,

CORS has been enabled in angular CLI now. Out-of-the-box, the Access-Control-Allow-Origin header is set to *. Not sure exactly when it was released, but for sure 1.0.0 and later have it enabled.

As with all CORS issues, your API server also need to be configured to accept CORS.

like image 76
Eric Liprandi Avatar answered Sep 20 '22 02:09

Eric Liprandi


Configuration to support CORS is done within the server, you will need to update your Spring API to allow requests from the CLI app which is hosted on port 4200 by default.

like image 30
Brocco Avatar answered Sep 23 '22 02:09

Brocco