javascript - 使用 XMLHttpRequest 获取非 utf8 数据

标签 javascript utf-8 xmlhttprequest

我想使用 xmlHttpRequest 从 Web 获取文档。但是,有问题的文本不是 utf8(在本例中是 windows-1251,但在一般情况下,我不确定)。

但是,如果我使用 responseType="text",它会将其视为字符串是 utf8,而忽略内容类型中的字符集(导致一团糟)。

如果我使用“blob”(可能是我想要的最接近的东西),我可以将其转换为考虑编码的 DomString 吗?

最佳答案

我实际上从这里找到了一个 API 来做我想做的事:

https://developers.google.com/web/updates/2014/08/Easier-ArrayBuffer-String-conversion-with-the-Encoding-API

基本上,使用 responseType="arraybuffer",从返回的 header 中选择编码,并使用 DataViewTextDecoder。它完全符合要求。

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/

相关文章:

java - 从 Java 调用 Javascript

validation - 批量 UTF-8 验证工具?

MySQL - 如何将整个数据库转换为 utf8

javascript - 不使用原生 XMLHttpRequest 的原因 - 为什么 $.ajax 是强制性的?

javascript - 在 Node 服务器中发布请求后更改浏览器 URL

javascript - 像在affinity.serif.com上滚动时,如何归档跳转到另一个div的文件?

javascript - getElementByID 使用最新的 KineticJS 更新返回 null

javascript - 用js构建正则表达式字符串

ios - 为什么 U+E006 (  ) 在 iOS 上显示为 T 恤?

javascript - XHR 带进度条的多文件上传