【发布时间】:2018-04-08 21:40:53
【问题描述】:
我正在开发一个应用程序,它具有来自 Angular2 和 后端 的 UI 作为在 Spring-boot 上完成的 rest api .
当我访问 URL 以获取用户列表时,它不会获取所有用户。当我检查浏览器控制台时,我收到以下错误:
Failed to load http://localhost:8080/api/user/getAll: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.
我们将不胜感激。
【问题讨论】:
-
是标头
Access-Control-Allow-Origin:*与响应一起从 Spring 应用程序发送。 Angular 与它无关。 -
这是后端的问题。您的后端应该允许来自所有来源的请求。
-
这是一个在 spring 上启用 cors 的示例,
https://spring.io/guides/gs/rest-service-cors/ -
@CrossOrigin 注释解决了我的问题。谢谢
标签: java angular rest spring-boot