首先,有两个导出xlsx接口,其中一个已经对接完成,另一个导出一直失败,遂查看原因;
导出成功的接口返回图如下:
导出失败的接口返回图如下:

此时我一直认为接口返回的数据格式有问题,然后去看我的代码,
接口代码:
javascript
// 问题导出
static chunkExport(data: any) {
return request<any>({
url: `${BASE_URL}/chunk/export`,
method: "post",
data,
responseType: "blob",
});
}
我发现我已经设置了 responseType: "blob",然后就去看我的request.ts代码,如下:
javascript
// 检查配置的响应类型是否为二进制类型('blob' 或 'arraybuffer'), 如果是,直接返回响应对象
if (response.config.responseType === "blob" || response.config.responseType === "arraybuffer") {
if (response.data.type !== "application/json") {
return response;
}
response.data = await handlerBlobJson(response.data);
}
随后,检查接口的响应标头中的Content-Type,
导出成功的Content-Type图如下:

导出失败的Content-Type图如下:

然后发现接口返回的Content-Type值错误,在我的响应拦截器被拦截,走了错误的逻辑,二进制文件当作 JSON 文本解析,导致JSON.parse报错。