首页 > 解决方案 > 使用 Angular 7 发布请求后出现 net::ERR_INVALID_HTTP_RESPONSE 错误

问题描述

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

我已经测试了我的服务,Postman并且一切都按预期工作。

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

但是httpclient返回错误:

[object Object]

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

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

我有两种方法(forPOST和 forGET)在注入到组件中的服务类中。它们看起来像这样:

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

更新 2:

向邮递员提出要求:

在此处输入图像描述


在此处输入图像描述

更新 3:尝试多次获取令牌显示有时发布请求成功,有时不成功...

在此处输入图像描述

标签: angularasp.net-core-webapiangular7

解决方案


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

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

在您的 ASP.NET Core 应用程序中,您能否暂时尝试一下解决方法:

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

Configure方法的开始。

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

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


推荐阅读