我有一个应用程序,它使用 Angular 7 作为前端,使用 ASP.Net Core 2.2 作为 API 服务。当我使用 Google Chrome 浏览器发送 POST
或 PUT
请求时,我收到了 ERR_INVALID_HTTP_RESPONSE
错误。
当我将 .Net Core 降级到 2.1 版时,一切正常
此外,当我在 Firefox 中测试我的应用程序时,再次一切正常!
我不知道如何解决这个问题
我使用的是谷歌浏览器 71 版
当我查看 Google Chrome 中的“网络”选项卡时,我发现 Chrome 也会向服务器发送预检请求...
以下是“网络”选项卡中记录的内容:
首先:
Request URL: http://localhost:12346/api/XXXX
Request Method: OPTIONS
Status Code: 204 No Content
Remote Address: [::1]:12346
Referrer Policy: no-referrer-when-downgrade
然后:(导致错误)
Request URL: http://localhost:12346/api/XXXX
Referrer Policy: no-referrer-when-downgrade
如您所见,chrome 不会向服务器(应该发送)发送任何 POST
请求。
最佳答案
如您所料,这可能与 ASP NET Core 2.2 有关。在 GitHub https://github.com/aspnet/AspNetCore/issues/4398 上注册了一个问题
解决方法如下 - 在您的 startup.cs
类中添加以下代码(我将其保存在 Configure
方法中)
app.Use(async (ctx, next) =>
{
await next();
if (ctx.Response.StatusCode == 204)
{
ctx.Response.ContentLength = 0;
}
});
一定要跟踪 GitHub 问题。
关于angular - ERR_INVALID_HTTP_RESPONSE 在 Google Chrome 浏览器中使用 Angular 7 和 ASP.Net Core 2.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53933830/