Ale
Ale

Reputation: 236

React JS with Spring boot API REST

I have a frontend webapp developed with React JS, and a rest API built with Spring boot. When I try to access to API from my react-app I get a 403 Error. My API Rest is deploy in tomcat server.

POST http://localhost:8080/fantasy/api/auth/signin 403
PostData.js:3 Uncaught (in promise) SyntaxError: Unexpected end of JSON input

Upvotes: 0

Views: 430

Answers (1)

Maciej Kowalski
Maciej Kowalski

Reputation: 26572

Most likely your ReactJs app and Spring Boot app are running under different ports and you did not configure CORS in the Spring Rest mapped methods in order enable incoming requests from the host:port on which ReactJs is running:

@CrossOrigin(origins = "http://localhost:4200")
@GetMapping("/user")
public Greeting getUser(){}

You can also define it on the Controller itself:

@CrossOrigin(origins = "http://localhost:4200")
@RestController

You can also register a separate CORS filter for entire application: reference

Upvotes: 2

Related Questions