赞
踩
利用window自带的atob()和btoa()方法
window.btoa("buzhichizhongwen!!!hahaha123")
window.atob("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9")
Uncaught DOMException: Failed to execute ‘btoa’ on ‘Window’: The string to be encoded contains characters outside of the Latin1 range. at :1:8
解决方案: 将数据进行URIEncoding及URIDecoding
//编码中文数据
btoa(encodeURIComponent("我是中文数据,hahaha"))
'JUU2JTg4JTkxJUU2JTk4JUFGJUU0JUI4JUFEJUU2JTk2JTg3JUU2JTk1JUIwJUU2JThEJUFFJUVGJUJDJThDaGFoYWhh'
//解码中文数据
decodeURIComponent(atob("JUU2JTg4JTkxJUU2JTk4JUFGJUU0JUI4JUFEJUU2JTk2JTg3JUU2JTk1JUIwJUU2JThEJUFFJUVGJUJDJThDaGFoYWhh"))
'我是中文数据,hahaha'
效果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。