简体   繁体   English

如何从 angular6/7 的帖子中获取大 json

[英]how to fetch large json from a post in angular6/7

I have migrated a piece of code to be able to export data as excel file in angular.我已经迁移了一段代码,以便能够以 angular 将数据导出为 excel 文件。 I assume the fact that the json is well formed and send from the server to the angular side.我假设json格式正确并从服务器发送到角度端。 I can see it in the network frame in th browser.我可以在浏览器的网络框架中看到它。 For small json, it's ok but when the size of the json starts to be large, the answer still failed.对于小json,可以,但是当json的大小开始变大时,答案仍然失败。 This following code corresponding to the service call下面这段代码对应服务调用

 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;
        }));
  }

and here, its the method which manages the return.在这里,它是管理回报的方法。

  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();
        });
  }

Currently, i do not manage to understand why it still finish in error and I get only "ok" in the console log.目前,我不明白为什么它仍然以错误方式完成并且我在控制台日志中只得到“确定”。 Any idea?任何的想法?

regards问候

Angular's HttpClientModule default response is a json. Angular 的 HttpClientModule 默认响应是一个 json。
Your problem is that you try to access the body property of the HTTP response, but Angular interprets that as you trying to access the body property in the body of the response.您的问题是您尝试访问 HTTP 响应的body属性,但 Angular 将其解释为您尝试访问响应正文中的body属性。

Remove observe and responseType from your post request and treat the response as a json.从您的发布请求中删除observeresponseType并将响应视为 json。 It should work.它应该工作。

find:找:
just need to use text as json只需要使用文本作为 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;      
          });
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM