使用readBlobAsText转化非utf8字符

我们知道在 fetch 请求中 json格式传输只能支持utf8语言,如果非uft8语言那怎么处理呢!

下面使用例子在处理非 application/x-www-form-urlencoded 下GBK的例子

const readBlobAsText = (blob, encoding) => {
    return new Promise((resolve, reject) => {
        const fr = new FileReader();
        fr.onload = (event) => {
            resolve(fr.result);
        };
        fr.onerror = (err) => {
            reject(err);
        };
        fr.readAsText(blob, encoding);
    });
};
export const _post = (url, body) => {
    const opt = {
        method: "POST",
        headers: {
            "content-type": "application/x-www-form-urlencoded",
        },
        body: body,
    };
    // application/json不支持非utf8字符集
    return fetch(url, opt).then((response) => {
        return response
            .blob()
            .then((blob) => readBlobAsText(blob, "GBK"))
            .then(JSON.parse);
    });
};