ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

TypeScript:将arraybuffer类型数据转换为json

2026/9/24 13:40:56 拓冰建站 浏览量
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));

             }

       });