net::ERR_INVALID_HTTP_RESPONSE post 请求 Angular 7 后出错

net::ERR_INVALID_HTTP_RESPONSE error after post request with Angular 7

我正在开发小型网络应用程序,我使用 angular 7 和 asp.net 核心网络 api 作为后端。我正在尝试使用 angular 发出 http post 请求。我的服务 returns 字符串(令牌),当我收到它时,我想在警告框中显示它。

我已经用 Postman 测试了我的服务,一切正常。

我的请求已从浏览器成功映射到控制器的操作方法。我在这个方法体中设置了断点,它成功地 returns 令牌没有任何问题。

但是httpclient returns错误:

[object Object]

在浏览器控制台中我看到了这个:

POST https://localhost:44385/api/auth net::ERR_INVALID_HTTP_RESPONSE

我有两种方法(用于 POSTGET) 在服务 class 中注入到组件中。它们看起来像这样:

logIn(username: string, password: string) {

    const encodedCredentials = btoa(username + ':' + password);

    const httpOptions = {
        headers: new HttpHeaders({
            "Authorization": "Basic " + encodedCredentials,
            "Access-Control-Allow-Origin":"*"
        }),
        responseType: 'text' as 'text'
    };

    this.http.post(this.urlBase + 'auth', null , httpOptions)
    .subscribe(result => {
        alert(result);
    }, error => {
        alert(error); // [object Object]
    });
}

get(){
    return this.http.get(this.urlBase + 'auth', {responseType: 'text'});
}

可能是什么问题?

更新:

HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: null, ok: false, …}
error: ProgressEvent
bubbles: false
cancelBubble: false
cancelable: false
composed: false
currentTarget: XMLHttpRequest {__zone_symbol__xhrSync: false, __zone_symbol__xhrURL: "https://localhost:44385/api/auth", __zone_symbol__loadfalse: null, __zone_symbol__errorfalse: null, __zone_symbol__xhrListener: ƒ, …}
defaultPrevented: false
eventPhase: 0
isTrusted: true
lengthComputable: false
loaded: 0
path: []
returnValue: true
srcElement: XMLHttpRequest {__zone_symbol__xhrSync: false, __zone_symbol__xhrURL: "https://localhost:44385/api/auth", __zone_symbol__loadfalse: null, __zone_symbol__errorfalse: null, __zone_symbol__xhrListener: ƒ, …}
target: XMLHttpRequest {__zone_symbol__xhrSync: false, __zone_symbol__xhrURL: "https://localhost:44385/api/auth", __zone_symbol__loadfalse: null, __zone_symbol__errorfalse: null, __zone_symbol__xhrListener: ƒ, …}
timeStamp: 80234.59999999614
total: 0
type: "error"
__proto__: ProgressEvent
headers: HttpHeaders
headers: Map(0) {}
lazyUpdate: null
normalizedNames: Map(0) {}
__proto__: Object
message: "Http failure response for (unknown url): 0 Unknown Error"
name: "HttpErrorResponse"
ok: false
status: 0
statusText: "Unknown Error"
url: null
__proto__: HttpResponseBase

更新二:

邮递员请求:


更新 3: 多次尝试获取令牌表明有时 post 请求成功,有时不成功...

找到问题所在了。它在服务器端。您使用 ASP.NET Core 2.2 吗?降级到 2.1 后,它终于可以工作了!

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.0"/>
    <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.0" PrivateAssets="All" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.1.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.0" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.0" />
  </ItemGroup>

  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
  </ItemGroup>
</Project>

但我不明白为什么。唯一改变的是一个 header,即 Server header。它从 Microsoft-IIS/10.0 (2.2) 更改为 Kestrel (2.1)

在 ASP.NET Core 2.2 中,我们发布了一个在 IIS 中运行的新服务器,用于 Windows 场景。您 运行 遇到的问题看起来像:https://github.com/aspnet/AspNetCore/issues/4398

发送 XMLHttpRequest 时,有一个 returns 状态代码 204 的预检 OPTIONS 请求。这被 IIS 服务器错误处理,向客户端返回了无效响应。

在您的 ASP.NET 核心应用程序中,您现在可以尝试解决方法吗:

app.Use(async (ctx, next) =>
{
  await next();
  if (ctx.Response.StatusCode == 204)
  {
    ctx.Response.ContentLength = 0;
  }
});

Configure 方法的开头。

这也将在 ASP.NET 核心的下一个补丁版本中修复。补丁发布后我会跟进

编辑: 最新版本 (2.2.1) 应该解决了这个问题:https://dotnet.microsoft.com/download/dotnet-core/2.2。请尝试查看问题是否已解决。

dotnet core 版本背后的问题。如果是 dotnet core 2.2.101 那么它就有问题了。我更改了最新版本,即 2.2.102 然后它解决了问题。同样,如果您获得最旧版本的 dotnet core<2.2.101,那么问题就解决了。