本文主要是介绍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));
}
});
这篇关于TypeScript:将arraybuffer类型数据转换为json的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!