angular - 为什么 httpbin.org 在 POST OR 请求中添加 API URL 我使用 angular cli proxy conf 来调用 API
问题描述
我们在本地环境中为 API 调用设置了代理配置,
{
"/api/*": {
"target": "https://172.16.2.10",
"secure": false,
"pathRewrite": {
"^/api/*": ""
},
"changeOrigin": true
}
}
然后使用 Angular http 服务调用 POST API,
this.http.post('/api/slots/1/apps/app-nat/flows?api_version=1', data).map((response: Response) => response.json());
当通过浏览器调用 API 时,httpbin.org 使用我们的基本 URL(api 调用 url)添加而不是 localhost:4200,就像这样,在浏览器中:http: //httpbin.org/api/slots/1/apps/app -nat/flows?api_version=1 // 发送 OPTIONS 请求。
注意:此问题仅适用于 POST 或 DELETE 请求,使用代理 conf 的 GET 请求工作正常。
我们的 GET API 请求示例(它的工作):
在代码中:
this.http.get('/api/slots/1/apps/app-nat/groups?api_version=1').map((response: Response) => response.json());
在浏览器中: http://localhost:4200/api/slots/1/apps/app-nat/flows?api_version=1
解决方案
我已经解决了这个问题,实际上我已经在 apiURL 的 environment.ts 文件中添加了 httpbin.org 这就是它显示问题的原因,我已经删除了它并在 apiUrl 中添加了 localhost:4200 现在它工作正常。
推荐阅读
- angular - OL 6.5.0,同一点图中的许多 LineString 会导致速度变慢
- python - 如何在 Calendly 中正确使用页面令牌进行迭代?
- python - 使用类似架构掩码的 u-net 进行图像分割时出错不起作用
- arrays - 在 mongoldb 中使用聚合管道弹出最后一个数组元素
- c# - MySqlCommand.Parameters.Add 在 c# 中的命令文本中添加斜杠
- json - 如何使用 JQ 减少 JSON 的值长度
- r - 如何根据某些条件匹配 R 中的项目?
- google-cloud-platform - 从 linux 谷歌云服务器下载文件到我的电脑
- python - 乌龟没有反应
- python - 不同维度张量的乘法