Razor88
Razor88

Reputation: 221

Angular 2, cannot read from localhost

I have followed the Angular 2 tour of Heroes tutorial to the T, and I have successfully implemented the displaying of the heroes (I renamed them to employees).

However, the tutorial uses an in memory mock web server. When I try to pass a localhost url (that displays json) from my spring-boot backend, I get this error:

XMLHttpRequest cannot load http://localhost:8080/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access

However, if I decide to use an online web api, for example :

https://jsonplaceholder.typicode.com/users

Then it works perfectly fine.

Any help would be greatly appreciated.

Upvotes: 0

Views: 693

Answers (1)

Razor88
Razor88

Reputation: 221

There was indeed duplicate questions (I posted since none of them seemed to have solved my problem). Turns out one of them did put me on the right track. I just had to put

@CrossOrigin(origin ="http://localhost:3000") 

on top of my controller class in Spring. This enabled CORS in spring.

For more details, look here

https://spring.io/blog/2015/06/08/cors-support-in-spring-framework

Upvotes: 1

Related Questions