TypeScript:将arraybuffer类型数据转换为json
通过axios发送http请求时,如果设置了
const httpArgs = {
method: 'GET',
url:/url/xxx,
params:{},
headers:{'Content-type':'application/octet-stream'},
responseType:'arraybuffer'
}
那么响应数据将被保存在arraybuffer类型的数组中,可以通过如下方式将其转为为json
const httpArgs = {
method: 'GET',
url:/url/xxx,
params:{},
headers:{'Content-type':'application/octet-stream'},
responseType:'arraybuffer'
};
axios.request(httpArgs).then(function (res) {
if(res.headers['content-type'].includes("application/json"))
{
const decoder = new TextDecoder('utf-8');
let retJson = JSON.parse(decoder.decode(res.data));
}
});