javascript atob returning 'String contains an invalid character'

后端 未结 3 1138
抹茶落季
抹茶落季 2020-12-13 09:58

I have an AJAX call getting info out of the Github API. It is returned in base64 encoding but when i try to decode it I get the aforementioned error.

Has anyone run

3条回答
  •  不思量自难忘°
    2020-12-13 10:27

    I have a better solution here:

    live demo: https://codepen.io/arliang/pen/mNQayE?editors=1111

    // https://best33.com/311.moe
    
    function base64encode(str) {
      let encode = encodeURIComponent(str).replace(/%([a-f0-9]{2})/gi, (m, $1) => String.fromCharCode(parseInt($1, 16)))
      return btoa(encode)
    }
    function base64decode(str) {
      let decode = atob(str).replace(/[\x80-\uffff]/g, (m) => `%${m.charCodeAt(0).toString(16).padStart(2, '0')}`)
      return decodeURIComponent(decode)
    }
    
    function tests() {
      Array.from([
        /*鼠、牛、虎、兔、龙、蛇、马、羊、猴、鸡、狗、猪*/
        '

提交回复
热议问题