使用Angular 7发布请求后,net :: ERR_INVALID_HTTP_RESPONSE错误

pub*_*lic 13 asp.net-core-webapi angular angular7

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

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

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

但httpclient返回错误:

[object Object]

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

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

我有两个方法(for POST和for GET)在服务类中注入组件.它们看起来像这样:

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'});
}
Run Code Online (Sandbox Code Playgroud)

有什么问题?

更新:

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
Run Code Online (Sandbox Code Playgroud)

更新2:

邮递员要求:

在此输入图像描述


在此输入图像描述

更新3:尝试多次获取令牌表明有时候帖子请求是成功的,有时候不是......

在此输入图像描述

Jus*_*lik 20

在ASP.NET Core 2.2中,我们发布了一个新的Server,它在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;
  }
});
Run Code Online (Sandbox Code Playgroud)

Configure方法的开头.

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

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