【问题标题】:AngularProxy to DotNetCore not working asking for CORSDotNet Core 的 Angular 代理无法正常工作,请求 CORS
【发布时间】:2018-11-05 17:04:21
【问题描述】:

我正在尝试将我的 angular-cli 应用设置为使用代理,因此我的网络服务器上不需要 CORS。

我的 DotNetCore 服务器在端口 49274 上运行。 我的 angular-cli 应用程序在 4200 上运行

我很确定代理可以消除 CORS 错误...

但我仍然收到 CORS 错误...

beer/list:1 Failed to load http://localhost:49274/api/beer: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.

core.js:1601 ERROR HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: null, ok: false, …}

这是我尝试过的配置列表...

{
  "/api": {
    "target": "http://localhost:49274",
    "secure": false
  }
}

{
  "/api/*": {
    "target": "http://localhost:49274",
    "secure": false
  }
}

{
  "/api": {
    "target": "http://localhost:49274",
    "secure": false,
    "changeOrigin": true
  }
}

{
  "/api/*": {
    "target": "http://localhost:49274",
    "secure": false,
    "changeOrigin": true
  }
}

{
  "/api": {
    "target": "http://localhost:49274",
    "secure": false,
    "changeOrigin": true,
    "pathRewrite": {"^/api" : "http://localhost:49274/api"}
  }
}


{
  "/api/*": {
    "target": "http://localhost:49274",
    "secure": false,
    "changeOrigin": true,
    "pathRewrite": {"^/api" : "http://localhost:49274/api"}
  }
}

{
  "/api": {
    "target": "http://localhost:49274",
    "secure": false,
    "changeOrigin": true,
    "pathRewrite": {"^/api" : ""}
  }
}

{
  "/api/*": {
    "target": "http://localhost:49274",
    "secure": false,
    "changeOrigin": true,
    "pathRewrite": {"^/api" : ""}
  }
}

【问题讨论】:

    标签: angular .net-core http-proxy


    【解决方案1】:

    该死的...仔细查看我的代码后...我发现错误在我的环境中。ts ....为一件简单的事情付出了这么多努力!

    我有

    export const environment = {
      production: false,
      apiUrl: 'http://localhost:49274/api/'
    

    };

    并且 apiUrl 应该是 '/api/' ...!我终于让它工作了:)

    【讨论】:

      猜你喜欢
      • 2015-09-16
      • 1970-01-01
      • 2020-01-21
      • 1970-01-01
      • 2020-10-07
      • 2014-11-03
      • 2014-04-21
      • 2017-04-06
      • 2018-07-13
      相关资源
      最近更新 更多