angular - 无法从我的 Angular 5 项目中访问 Spring Boot Rest API
问题描述
我正在准备一个基于微服务的示例应用程序,我想在其中使用用 Spring Boot 编写的后端 API。现在我希望它从我的 Angular 5 UI 应用程序中运行。
但低于错误。我做了 ctrl+F12 并且从控制台我得到了以下错误。
无法加载http://localhost:8080/contacts:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问源“ http://localhost:4200 ”。
解决方案
I suggest to use an angular proxy configuration.
1) Create a new file in the frontend root directory (next to package.json file). You can name is file whatever you want, proxy.config.json for example. This files contains a javascript object.
{
"/api/*": {
"target": "http://localhost:8080",
"secure": false
}
}
2) Change the 'ng start' script into package.json to "start": "ng serve --proxy-config proxy.config.json"
3) Restart the cli server (if necessary)
Each calls from your angular-cli server to /api will be redirected to http://localhost:8080/api . This way, yo don't have to touch the CORS config and will not affect the production security.
推荐阅读
- agda - Loading files in Agda: unclear explanation in Learn you an Agda
- r - R Session aborted after querying data from an AWS Athena Table
- android - 如何在 Android 上以编程方式更改 Material Components 中的颜色?
- python - 从 sqlite 数据库中获取多行,合并共享相同日期的行,并在表中打印
- java - 如何在 Java 或 Kotlin 中解析 JSON 映射,其中映射具有键
- vue.js - 无法验证对象数组中每个电子邮件值字段上的电子邮件
- sql - 根据间隔(月/季)查找下一个日期
- spring - 无法从命令行编译 Spring Framework 代码
- java - 如何通过在 lambda 函数中收集 IntStream 来操作值
- javascript - 在刀片脚本 laravel 中使用循环紧凑设置最大输入