首页 > 解决方案 > 发出多个同步ajax请求的最佳方法是什么

问题描述

我将 xlsx 转换为 json 并将这些行发送到后端处理的 ajax 请求。

excel 有 5k 行,我使用 concatMap 执行请求。还有另一种方法吗?我读过浏览器每个主机名最多有 6~12 个连接。

当我测试这个解决方案时,它在 6 分钟内执行了所有请求,每次使用 10 个请求。

浏览器真的每个主机最多有 6 个连接吗?

有人已经提出了相同的解决方案吗?

--

上面有一个我的解决方案的例子

let ids = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let arrIds = this.chunkArray(ids, 3);

for(let i = 0; i < arrIds.length; i++) {
  this.getSomethingFromAnAPI(arrIds[i]).subscribe(response => {
      console.log(response.name)
      this.data.push({
        body: response.name, 
        title: response.name, 
        id: response.id
      });
    }, error => {
      console.error(error);
    });
 }

//Send line to api
public getSomethingFromAnAPI(ids: number[]): any {
      return from(ids).pipe(
        concatMap(id => <Observable<any>> this.http.post('SEND_LINE_API'))
      ); 
  }

标签: ajaxangularrestbrowserrequest

解决方案


推荐阅读