简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM