0

My application built on Angular + NGRX, fetching data from server. Server is hosting services built on Java Spring.

Spring services are hosted on a specific domain, thus in angular we are accessing them using specific url say

 http://JavaService-hostedDomain/ServiceName

In spring specific Controllers are built for them @GetMapping(/ServiceName)

Now issue what i am facing is that to view the application, i have to disable chrome web security using

--disable web security chrome command to launch chrome then application is loading correctly.

On running in chrome without disabling Chrome web security, i am getting following error

XMLHttpRequest cannot load http://localhost:4200/url. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

How to resolve above CORS error, is changes required on Angular(Client Side) or Java (Server side) ?

If possible any way to resolve this by making changes only on Angular side ?

Julius Dzidzevičius
  • 10,775
  • 11
  • 36
  • 81
Olivia
  • 125
  • 4
  • 13

1 Answers1

0

I had that problem many times and it must be resolved from server side because I don't know about java but you need to set which origins can be accepted for making request to your server because the browser block all the attempts for security.

For example in Laravel (PHP) I installed a library that manages the CORS as follows :

/*
|--------------------------------------------------------------------------
| Laravel CORS
|--------------------------------------------------------------------------
|
| allowedOrigins, allowedHeaders and allowedMethods can be set to array('*')
| to accept any value.
|
*/

'supportsCredentials' => false,
'allowedOrigins' => ['*'], // HOST allowed for making request
'allowedOriginsPatterns' => [],
'allowedHeaders' => ['*'],
'allowedMethods' => ['GET', 'POST', 'PUT',  'DELETE', 'OPTIONS'],
'exposedHeaders' => [],
'maxAge' => 0,
'hosts' => ['*']

I hope the above can give you an idea

Check this for java spring: https://spring.io/guides/gs/rest-service-cors/

Regards

Daniel Luna
  • 341
  • 2
  • 9