1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > javascript使用btoa和atob来进行Base64转码和解码

javascript使用btoa和atob来进行Base64转码和解码

时间:2020-10-21 22:11:59

相关推荐

javascript使用btoa和atob来进行Base64转码和解码

javascript中如何使用Base64转码

let str = 'javascript';let btoaStr = window.btoa(str); //转码结果 amF2YXNjcmlwdA==console.log(btoaStr);console.log(window.atob(btoaStr)); //解码结果 javascript

Base64转码的对象只能是字符串,

var str = "China,中国"; window.btoa(str) ;

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

那么如何让他支持汉字呢?

let str = 'javascript,博客';let btoaStr = window.btoa(window.encodeURIComponent(str)); //转码结果 amF2YXNjcmlwdCUyQyVFNSU4RCU5QSVFNSVBRSVBMg==console.log(btoaStr);

console.log(window.decodeURIComponent(window.atob(btoaStr))); //解码结果 javascript,博客

这就是btoa 和 atob的简单实用,希望对大家有所帮助。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。