From Docs
Angular developers may encounter a cross-origin resource sharing error
when making a service request (typically a data service request) to a
server other than the application's own host server. Browsers forbid
such requests unless the server permits them explicitly.
There isn't anything the client application can do about these errors.
The server must be configured to accept the application's requests.
Read about how to enable CORS for specific servers at enable-cors.org
To enable CORS on Spring you can refer CORS with Spring
Alternate Approach
Proxy To Backend Setup
Setup a proxy for API calls for your Angular CLI app
"/api/*": {
"target": "https://localhost:8000",
"secure": false,
"logLevel": "debug",
"pathRewrite": {
"^/api": ""
},
"changeOrigin": true
}
"api/*"
:
All requests made to /api/
from within your application will be forwarded to
target": "https://localhost:8000/api
"secure": false,
:
A backend server running on HTTPS with an
invalid certificate will not be accepted by default. If you want to,
you need to set secure: false
.
"logLevel": "debug"
To help debug whether or not your proxy
is working properly, you can also add the logLevel option as
follows: Possible options for logLevel include debug
, info
,
warn
, error
, and silent
(default is info).
"pathRewrite": {
"^/api": "" },
pathRewrite setting says that if the path matches ^/api
(i.e. if it starts with /api) then rewrite that portion with the empty string (i.e. remove it from the path), so all the request to https://localhost:8000/api
will go to https://localhost:8000
"changeOrigin": true
: If you need to access a backend that is not on localhost or when you’re using some virtual proxies (such as configured with Apache2) on your backend set it to true.
- `target:
proxy options
provided in this package is from underlying node-http-proxy
Proxying Support might help you to get rid off some CORS
exceptions during the development stage but There isn't much the client application can do about these exceptions The server must be configured to accept the application's requests.
I want to add CORS support to my server
NOTE:
The proxy
configuration is intended to proxy calls when running the
dev server via ng serve
. After you run ng build
you are
responsible for the web server and its configurations.
CORS
How to create a Minimal, Reproducible Example – Vikas