Angular 7发布请求后出现net :: ERR_INVALID_HTTP_RESPONSE错误 [英] net::ERR_INVALID_HTTP_RESPONSE error after post request with Angular 7

查看:1521
本文介绍了Angular 7发布请求后出现net :: ERR_INVALID_HTTP_RESPONSE错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on small web application and I am using angular 7 and asp.net core web api as a back end. I am trying to make http post request with angular. My service returns string (token) and when I receive it, I want to show it in alert box.

我已经使用Postman测试了我的服务,并且一切正常.

I have tested my service with Postman and everything works as expected.

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

From the browser my request is successfully mapped to the controller's action method. I have set breakpoint in this method body and it successfully returns token without any problem.

但是httpclient返回错误:

But httpclient returns error:

[object Object]

在浏览器控制台中,我看到以下内容:

And in the browser console I see this:

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

我有两种方法(对于注入组件的服务类中的POSTGET).它们看起来像这样:

I have two methods (for POST and for GET) in a service class injected into component. They look like these:

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:尝试多次获取令牌表明,有时发布请求成功完成,有时没有成功...

Update 3: trying to get token several times showed that sometimes post request is successfull and sometimes not...

推荐答案

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

In ASP.NET Core 2.2, we released a new Server which runs inside IIS for Windows scenarios. The issue you are running into looks like: https://github.com/aspnet/AspNetCore/issues/4398.

发送XMLHttpRequest时,会有一个预检OPTIONS请求,该请求返回状态码204.IIS服务器不正确地处理了该请求,从而向客户端返回了无效的响应.

When sending the XMLHttpRequest, there is a preflight OPTIONS request which returns a status code of 204. This was incorrectly handled by the IIS server, returning an invalid response to the client.

在您的ASP.NET Core应用程序中,您可以暂时尝试解决方法吗:

In your ASP.NET Core application, can you please try the workaround for now:

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

方法开头的

.

in the beginning of Configure method.

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

This will also be fixed in the next patch release of ASP.NET Core. I will follow up when the patch is released.

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

The latest release (2.2.1) should address this problem: https://dotnet.microsoft.com/download/dotnet-core/2.2. Please try and see if the issue is resolved.

这篇关于Angular 7发布请求后出现net :: ERR_INVALID_HTTP_RESPONSE错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆