首页 > 解决方案 > 如何从 angular6/7 中的帖子中获取大 json

问题描述

我已经迁移了一段代码,以便能够以角度将数据导出为 excel 文件。我假设 json 格式正确并从服务器发送到角度端。我可以在浏览器的网络框架中看到它。对于小json,没关系,但是当json的大小开始变大时,答案仍然失败。以下代码对应于服务调用

 exportSynthesis(recordId: number, moduleId: number) {
    const body = null;

    return this.http.post(this.apiUrl + `/data`
      + `${recordId}/module/${moduleId}`, body,
      { 
        headers: new HttpHeaders({ 'Content-Type': 'application/json' }), 
        observe: 'response', responseType: 'json' }).pipe(
        map((resp: any) => {
          return resp.body;
        }));
  }

在这里,它是管理退货的方法。

  exportSynthesis() {
    this.service.exportSynthesis(this.recordId, this.moduleId)
      .subscribe(
        (exportResult) => { this.exportResult = exportResult; },
        err => {
          console.log('err:', err);
          this.errorHandlerService.handleError('failed', err);
          
        },
        () => {
          console.log('json:', this.exportResult);
          const worksheet: XLSX.WorkSheet = XLSX.utils.json_to_sheet(this.exportResult);
          const workbook: XLSX.WorkBook = { Sheets: { 'data': worksheet }, SheetNames: ['data'] };
          const excelBuffer: any = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' });

          const blob = new Blob([excelBuffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8' });
          const url = window.URL.createObjectURL(blob);
          const a = document.createElement('a');
          a.href = url;
          a.download = '(GEO) ' + this.record.label + ' - name.xlsx';
          a.click();
          window.URL.revokeObjectURL(url);
          a.remove();
        });
  }

目前,我不明白为什么它仍然错误地完成,我在控制台日志中只得到“ok”。任何想法?

问候

标签: jsonangularlarge-data

解决方案


find:
只需要使用文本作为json

    return this.http.post(this.apiUrl + `/geo/v1/synthesis/xls/record/`
      + `${recordId}/module/${moduleId}`, body,
      { 
        headers: headers, 
        observe: 'response',
         responseType:  'text' as 'json'}).
          map((resp: any) => {
            return resp.body;      
          });
  }

推荐阅读