我想通过单击我的 Angular 页面中的按钮来下载 Excel 文件。
当用户单击按钮
时,单击事件会触发服务方法调用:
downloadExcel(){
this.componentService.getExel().subscribe(
data => {
console.log("To Download:", data); // this part is not executed, throwing Syntax Error* shown below
//Here, function called to download excel-file with data
}
);
}
服务:
getExel(){
let url = 'http://url/endpoint/';
return this.httpClient.get(url);
}
**语法错误:
error: SyntaxError: Unexpected token P in JSON at poisition 0 at JSON.parse
在 Django REST Framework 端:
class GetExcel(APIView):
def get(self, request, format=None):
with open('path/to/excel/file', 'rb') as tempfile:
tempdata = tempfile()
ResponseData = HttpResponse(tempdata, content_type='application/vnd...')
ResponseData['Conetent-Disposition']="attachment; filename=%s" %Filename
return ResponseData
所以,我无法弄清楚我在哪里犯了错误。请告诉我如何修复它。
最佳答案
...JSON.parse 的位置 0 处出现意外的 JSON 标记 P
因此,在响应对象中,HTTPClient api 尝试解析来自 httpClient.get(url)
调用的响应,存在 JSON 格式错误。问题出在 data
对象中,但它是由 HTTP 响应本身格式错误引起的。
关于excel - 尝试下载 Excel 文件时,Angular4 中的响应数据出现语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49654827/