【问题标题】:Failed to get data when accessing the rest-api method getAll [duplicate]访问rest-api方法getAll时获取数据失败[重复]
【发布时间】: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


【解决方案1】:

如果前端 (Angular2) 和后端 (Boot) 位于两个不同的域中,则解决方案是在后端启用 cors。在这里阅读:https://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/html/cors.html 和这里https://spring.io/guides/gs/rest-service-cors/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-05
    • 2020-04-09
    • 1970-01-01
    • 2011-06-07
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多