Angular 4 HttpClient - 多个请求

时间:2017-10-26 08:05:58

标签: angular parallel-processing angular4-httpclient

我的问题是来自Angular 4的HttpClient对服务器的多个请求。简单的测试代码:

import {Component} from '@angular/core';
import {HttpClient} from '@angular/common/http';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';

  constructor(private http: HttpClient) {
  }

  startTest(): void {
    this.http.get('/dictionary/1.0/dictionary/pl/events', {
      observe: 'body',
    }).subscribe(
      (data) => {
        console.log('Data', data);
      }
    );
    this.http.get('/dictionary/1.0/dictionary/pl/event_state', {
      observe: 'body'
    }).subscribe(
      (data) => {
        console.log('Data', data);
      }
    );
    this.http.get('/dictionary/1.0/dictionary/pl/event_sentby_categories', {
      observe: 'body'
    }).subscribe(
      (data) => {
        console.log('Data', data);
      }
    );
  }
}

结果至少有一个请求将成功执行,但其余请求将失败并显示错误“JSON解析错误:意外的EOF”。

看起来后续呼叫休息服务正在取消当前存在(未完成)的呼叫。我看到使用Observable.forkJoin的示例并行发出请求它们也不起作用。我尝试过不同的浏览器(全部在Mac上),效果是一样的。

0 个答案:

没有答案