如何在angular6/7中从帖子中获取大型json
我已经迁移了一段代码,以便能够将数据以excel文件的形式导出到angular中。 我假设json格式良好,从服务器发送到服务器端。我可以在浏览器的网络框架中看到它。 对于小json,这是可以的,但是当json的大小开始变大时,答案仍然是失败的。 以下代码对应于服务调用如何在angular6/7中从帖子中获取大型json,json,angular,large-data,Json,Angular,Large Data,我已经迁移了一段代码,以便能够将数据以excel文件的形式导出到angular中。 我假设json格式良好,从服务器发送到服务器端。我可以在浏览器的网络框架中看到它。 对于小json,这是可以的,但是当json的大小开始变大时,答案仍然是失败的。 以下代码对应于服务调用 exportSynthesis(recordId: number, moduleId: number) { const body = null; return this.http.post(this.apiU
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”。
有什么想法吗
Angular的HttpClientModule默认响应是json。
您的问题是您试图访问HTTP响应的
body
属性,但Angular将其解释为您试图访问响应主体中的body
属性
从post请求中删除observe
和responseType
,并将响应视为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;
});
}
JSON对象是否包含“body”属性?就像在
{body:[/*您的数据*/]}
中一样?我这样问是因为您的服务中有管道(map((resp:any)=>{return resp.body;}))
。另外,在这两种情况下(小json和长json),它可能有助于显示您的控制台输出。您的服务应该是这样的返回this.http.post([…]).pipe((res)=>res.body)代码>。在[…]范围内,您需要从观察@Askirkela,nop移除管道。我的json类似于{[/*data*/]}直接返回。好的@Marek W,我将测试itSorry@MarekW,但我已经重新检查,管道已经在返回后。你能给我解释一下你的想法吗?因为我不太清楚