AngularProxy到DotNetCore没有工作要求CORS

时间:2018-05-26 14:25:19

标签: angular .net-core http-proxy

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

我的DotNetCore服务器在端口49274上运行。 我的angular-cli app运行于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" : ""}
  }
}

1 个答案:

答案 0 :(得分:0)

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

我有

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

};

和apiUrl应该是' / api /' ...!我终于开始工作了:))