后端返回代码 0,body 是:[object ProgressEvent] - Angular 5

Backend returned code 0, body was: [object ProgressEvent] - Angular 5

在我的 angular application 中,我正在呼叫一个 API,它需要 超过 2 分钟 才能响应,但在 2 分钟后,angular 抛出以下错误;

Access to XMLHttpRequest from origin has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Backend returned code 0, body was: [object ProgressEvent]

Failed to load resource: net::ERR_FAILED

我已经尝试了我在互联网上找到的所有可能的解决方案,非常喜欢;

  1. 添加服务调用超时;

    return this.httpClient.post<ViewModelResponse>(this.baseUrl + this._getMetaDataUrl, { headers: this.configurations.getHeaderWithAuth() }).timeout(300000);

  2. 用管道添加超时

    return this.httpClient.post<ViewModelResponse>(this.baseUrl + this._getMetaDataUrl, { headers: this.configurations.getHeaderWithAuth() }).pipe(timeout(300000));

  3. 实现 HttpInterceptor

  4. 在 headers "timeout": ${300000}

    中添加超时
  5. IIS 服务器超时增加到 20 分钟..

  6. 人们还建议更改代理文件,但我没有任何代理文件..

但对我来说没有任何效果.. 一旦 API 响应超过 2 分钟,所有这些解决方案都失败了。我在后端使用 dotnet core,它每次都 returns 正确响应。

In case of Pipe(timeout()), call was at backend but angular throws error that response is null

我们将不胜感激。

几天前我遇到了同样的问题。 问题绝对是后端。 您必须在 ConfigureServices 中添加指令,在 Startup class 中。 Here 是我用来修复错误的微软文档。如果使用精确的 url 不起作用,您应该像这样使用 allorigin 策略: builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader();

经过大量搜索,我终于找到了我的问题。正如大家所说,这是一个 后端服务器 (Dotnet Core) 问题。

Setting the RequestTimeout="00:20:00" on the aspNetCore tag in web.config and deploying the site resolved my problem.

This article has details of adding timeout in dotnet core application.