首页 > 解决方案 > 托管在 localhost 上的 ionic 请求 api 中的 CORS 问题

问题描述

我的 Ionic 应用程序正在运行http://localhost:8100/,我正在请求http://localhost:40410/api/myApiapi。
我知道它会出现 CORS 问题,我尝试通过添加代理ionic.config.json设置

"proxies": [
{
  "path": "/api",
  "proxyUrl": "http://localhost:40410/"
}
           ]  

当我ionic serve在控制台上得到这个输出时Proxy added:/api => https://localhost:44381/ ,但在应用程序中,我无法从 api 获取任何数据。
这就是我在 chrome 开发工具中得到的

Request URL: http://localhost:8100/api/myApi
Request Method: GET
Status Code: 500 Internal Server Error
Remote Address: 127.0.0.1:8100

这是我的 ts 代码,

    this.http.get('/api/myApi').subscribe(
(data) => {
  console.log(data.json());
},(error) =>{
console.log(error);
}
   );

标签: apiionic-frameworkionic3

解决方案


您可以使用以下参数运行 chrome 以禁用 cors 保护。--禁用网络安全


推荐阅读