Uncaught DOMException: Failed to execute ‘btoa’ on ‘Window’

在使用js btoa 编码为base64的时候

报错提示

Uncaught DOMException: Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.

只有含有中文的时候才会报错,如果是英文和数字的话就不会有错误

这个问题前段时间就遇到过一次了,然后网上查了一下解决了,今天再一次遇到,突然有点懵,于是查看了之前的代码

解决办法

只需要btoa在中间再套一层编码转换,使用encodeURI转换一下编码即可

btoa(encodeURI('哩哩啦啦'));

使用atob解码base64时候同理,也需要套一层decodeURI解码

decodeURI(atob('base64'))

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注




Enter Captcha Here :