首页 > 解决方案 > 为什么 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

标签: angularpostproxyrequest

解决方案


我已经解决了这个问题,实际上我已经在 apiURL 的 environment.ts 文件中添加了 httpbin.org 这就是它显示问题的原因,我已经删除了它并在 apiUrl 中添加了 localhost:4200 现在它工作正常。


推荐阅读