Angular不会在我创建的代理上运行,并将其默认本地主机放在我的api之前

Angular不会在我创建的代理上运行,并将其默认本地主机放在我的api之前,angular,Angular,我在创建的文件中创建了一个代理,如下所示: { "/api/*": { "target": "https://localhost:44361", "secure": false, "logLevel": "debug", "changeOrigin": true } } 在my package.json中,我编辑如下: "start": "ng serve --proxy-config proxy.config.json", 重新启动项目后,它会在新代理中成功构建,但在我运行

我在创建的文件中创建了一个代理,如下所示:

{
"/api/*": {
  "target": "https://localhost:44361",
  "secure": false,
  "logLevel": "debug",
  "changeOrigin": true
}
}

在my package.json中,我编辑如下:

"start": "ng serve --proxy-config proxy.config.json",
重新启动项目后,它会在新代理中成功构建,但在我运行的项目中:

    getPosts(x){  

   // return  this.http.post(this.url,{params:{username:x}});
    return this.http.post('/api/Auth/token/', "morteza")
  .pipe(map(user => {
  // login successful if there's a jwt token in the response
  if (user ) {
  // store user details and jwt token in local storage to keep user logged in between page refreshes
  localStorage.setItem('TokenInfo', JSON.stringify(user));
  }

   return user;
  }));
它不会在创建的代理上应用我的url,而是在默认的localhost:4200上运行。您可以尝试以下操作:

{
"/api/*": {
  "target": "https://localhost:44361",
  "secure": false,
  "logLevel": "debug",
  "changeOrigin": true,
  "pathRewrite": { "^/api": "" }
}

这看起来是正确的。“网络”选项卡中的请求仍将显示localhost:4200 url,但它将被代理到您在配置中设置的targeturl。您是否从api中得到错误?您的api是否映射到localhost:44361/api/Auth/token/或localhost:44361/Auth/token/?@FrankAdrian我这样做是为了摆脱被CORS阻止的策略:No“Access Control Allow Origin,但现在它在localhost:4200\/api/Auth/token/上运行,并显示错误500很好,这意味着错误来自服务器,您是否在api上定义了api路径?也许可以尝试将
“pathRewrite”:{“^/api”:“}
添加到您的配置中。@FrankAdrian我应该在哪里编写它?如果没有帮助,请查看服务器和angular应用程序的控制台,它应该会为您提供有关500错误的更多信息。我得到zone.js:3243 POST 404(未找到)我得到相同的东西404未找到,我不得到错误500ah,您在目标url中有https,它应该是http吗?但是当我在https上运行其时,我的后端APi(.NetCore)会不会是一个“证书”问题?检查此文档(安全选项)