简体   繁体   English

使用Spring Boot API REST反应JS

[英]React JS with Spring boot API REST

I have a frontend webapp developed with React JS, and a rest API built with Spring boot. 我有一个用React JS开发的前端webapp,还有一个用Spring boot构建的rest API。 When I try to access to API from my react-app I get a 403 Error. 当我尝试从我的react-app访问API时,出现403错误。 My API Rest is deploy in tomcat server. 我的API Rest已部署在tomcat服务器中。

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: 您的ReactJs应用程序和Spring Boot应用程序很可能在不同的端口下运行,并且您没有在Spring Rest映射方法中配置CORS以便启用运行ReactJs的host:port的传入请求:

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

You can also define it on the Controller itself: 您也可以在Controller本身上定义它:

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

You can also register a separate CORS filter for entire application: reference 您还可以为整个应用程序注册一个单独的CORS过滤器: 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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