Home  >  Q&A  >  body text

javascript - es6 fetch gbk网页是乱码怎么解决

比如在谷歌打开贴吧网页后,在控制台输入 fetch('http://tieba.baidu.com').then(res=>res.text()).then(html=>console.log(html)),
因为贴吧用的是gbk编码,fetch出来的结果乱码了,怎么解决?谢谢!!!

阿神阿神2749 days ago1074

reply all(1)I'll reply

  • 大家讲道理

    大家讲道理2017-04-10 16:55:18

    fetch('http://tieba.baidu.com')
        .then(res=> res.blob())
        .then(blob => {
            var reader = new FileReader();
            reader.onload = function(e) {
              var text = reader.result;
              console.log(text)
            }
            reader.readAsText(blob, 'GBK') 
        })

    reply
    0
  • Cancelreply