HttpClient-如何请求非JSON数据 [英] HttpClient - How to request non-JSON data
本文介绍了HttpClient-如何请求非JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过api下载excel.我尝试了此方法,但不幸的是我收到了此错误
I want to download excel through api ..i tried this method but unfortunately im getting this error
SyntaxError:JSON中意外的令牌P在XMLHttpRequest.onLoad上JSON.parse()位置0处(我的代码在这里
getSalesARExcel(attach: string): Observable<any>{ const obj={ "AttachmentId":attach } return this.http.post(baseUrl + 'api/report/aging/receivables/summary/download/xlsx', obj ,{ headers: new HttpHeaders({ 'Content-Type': 'application/json', "Authorization":'Bearer ' + localStorage.getItem('token'), 'Access-Control-Allow-Origin':'*', 'Access-Control-Allow-Headers':'Origin, Methods, Content-Type', 'responseType': 'ResponseContentType.Blob' }) }) } this.excel.getSalesARExcel(value.attach).subscribe(res => { console.log("excel", res) this.downloadExcelFile(res); }) downloadExcelFile(data: any){ var blob = new Blob([data], { type: 'application/vnd.ms-excel' }); var url= window.URL.createObjectURL(blob); window.open(url); }
推荐答案
删除CORS标头,并使
responseType
为选项,而不是标头:Remove the CORS headers and make
responseType
an option, not a header:
getSalesARExcel(attach: string): Observable<any>{ const obj={ "AttachmentId":attach } return this.http.post(url, obj ,{ headers: new HttpHeaders({ 'Content-Type': 'application/json', "Authorization":'Bearer ' + localStorage.getItem('token'), ̶'̶A̶c̶c̶e̶s̶s̶-̶C̶o̶n̶t̶r̶o̶l̶-̶A̶l̶l̶o̶w̶-̶O̶r̶i̶g̶i̶n̶'̶:̶'̶*̶'̶,̶ ̶'̶A̶c̶c̶e̶s̶s̶-̶C̶o̶n̶t̶r̶o̶l̶-̶A̶l̶l̶o̶w̶-̶H̶e̶a̶d̶e̶r̶s̶'̶:̶'̶O̶r̶i̶g̶i̶n̶,̶ ̶M̶e̶t̶h̶o̶d̶s̶,̶ ̶C̶o̶n̶t̶e̶n̶t̶-̶T̶y̶p̶e̶'̶,̶ ̶'̶r̶e̶s̶p̶o̶n̶s̶e̶T̶y̶p̶e̶'̶:̶ ̶'̶R̶e̶s̶p̶o̶n̶s̶e̶C̶o̶n̶t̶e̶n̶t̶T̶y̶p̶e̶.̶B̶l̶o̶b̶'̶ }), 'responseType': 'blob' }) }
CORS标头是响应标头,而不是请求标头.
CORS headers are response headers, not request headers.
responseType
是XHR选项,而不是标头.The
responseType
is an XHR option, not a header.有关更多信息,请参见
这篇关于HttpClient-如何请求非JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文