我想使用 xmlHttpRequest
从 Web 获取文档。但是,有问题的文本不是 utf8(在本例中是 windows-1251,但在一般情况下,我不确定)。
但是,如果我使用 responseType="text"
,它会将其视为字符串是 utf8,而忽略内容类型中的字符集(导致一团糟)。
如果我使用“blob”(可能是我想要的最接近的东西),我可以将其转换为考虑编码的 DomString 吗?
最佳答案
我实际上从这里找到了一个 API 来做我想做的事:
基本上,使用 responseType="arraybuffer"
,从返回的 header 中选择编码,并使用 DataView
和 TextDecoder
。它完全符合要求。
const xhr = new XMLHttpRequest();
xhr.responseType = "arraybuffer";
xhr.onload = function() {
const contenttype = xhr.getResponseHeader("content-type");
const charset = contenttype.substring(contenttype.indexOf("charset=") + 8);
const dataView = new DataView(xhr.response);
const decoder = new TextDecoder(charset);
console.log(decoder.decode(dataView));
}
xhr.open("GET", "https://people.w3.org/mike/tests/windows-1251/test.txt");
xhr.send(null);
fetch("https://people.w3.org/mike/tests/windows-1251/test.txt")
.then(response => {
const contenttype = response.headers.get("content-type");
const charset = contenttype.substring(contenttype.indexOf("charset=") + 8);
response.arrayBuffer()
.then(ab => {
const dataView = new DataView(ab);
const decoder = new TextDecoder(charset);
console.log(decoder.decode(dataView));
})
})
关于javascript - 使用 XMLHttpRequest 获取非 utf8 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46753212/