Languages

React + Spring Controller - CORS policy: No 'Access-Control-Allow-Origin' header is present

4 points
Asked by:
Hayley-Mooney
677

I write an app with React + Java (Spring Framework) and encountered problem with AJAX request to Spring Controller.

Error from browser console:

Access to fetch at 'http://localhost:8080/users/get-users' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

React AJAX request:

How to fix this problem?

1 answer
4 points
Answered by:
Root-ssh
178180

It looks like you try to do cross origin request that is not permitted by default in modern web browsers.

You need to add @CrossOrigin annotation to your controller to let the sever accept requests from different origins, in your case localhost:3000.

Check this:

0 commentsAdd comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join