Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
307 views
in Technique[技术] by (71.8m points)

javascript atob returning 'String contains an invalid character'

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 into this before and know what is going on? The info the server is returning runs fine through web based decoders. Maybe it get s mangled a bit in a variable? IDK :/

(function () {
    'use strict';

    var objectsList = [];
    var isParsing = 0;
    var insertArea = $('body');

    function makeAJAXCall(hash, cb) {
        $.ajaxSetup({
            Accept: 'Application/vnd.github.raw+json',
            dataType: 'jsonp'
        });

        $.ajax({
            url: hash,
            success: function (json) {
                if (cb) {
                    cb(json);
                }
            },
            error: function (error) {
                console.error(error);
                throw error;
            }
        });
    }

    function parseBlob(hash, cb) {
        makeAJAXCall(hash, function (returnedJSON) {  // no loop as only one entry
            if (cb) {
                cb(returnedJSON.data.content);
            }
        });
    }

    function addSVGToPage(SVGToAdd) {
        var entry, decodedEntry;
        makeAJAXCall(SVGToAdd, function (returnedJSON) {
            console.info(window.atob(returnedJSON.data.content));
        });
    }

    function parseTree(hash) {
        var i, entry;
        var tree = 'https://api.github.com/repos/myusername/SVG-Shapes/git/trees/' + hash;

        makeAJAXCall(tree, function (returnedJSON) {
            for (i = 0;  i < returnedJSON.data.tree.length; i += 1) {
                entry = returnedJSON.data.tree[i];

                if (entry.type === 'blob') {
                    if (entry.path.slice(-4) === '.svg') {     // we only want the svg images not the ignore file and README etc
                        addSVGToPage(entry.url);
                    }
                } else if (entry.type === 'tree') {
                    parseTree(entry.sha);
                }
            }
        });
    }

    $(document).ready(function () {
        parseTree('master');
    });
}());

Edit: below is a sample of what is returned from a console.info(returnedJSON.data.content);

PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KPCFET0NU WVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjEvL0VOIiAiaHR0 cDovL3d3dy53My5vcmcvR3J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gk8L2RlZnM+CgoJPHJlY3Qgd2lkdGg9IjQwMCIg aGVpZ2h0PSIyNCIgeD0iMCIgeT0iMCIgc3R5bGU9InN0cm9rZS13aWR0aDoy cHg7c3Ryb2tlOmdyZXk7Ii8+ICA8IS0tIEJhY2tpbmcgcmVjdC9mcmFtZSAt LT4KCgk8ZyBjbGlwLXBhdGg9InVybCgjbG9hZGNsaXApIj4KCQk8cmVjdCB4 PSItMTgiIHkgPSIyIiB3aWR0aD0iNTE2IiBoZWlnaHQ9IjIwIiBzdHlsZT0i c3Ryb2tlLXdpZHRoOjBweDtzdHJva2U6Z3JleTsiIGZpbGw9InVybCgjc3Ry aXBlZGJhY2spIj4KCQkJPGFuaW1hdGVUcmFuc2Zvcm0KCQkgICAgICAgIGF0 dHJpYnV0ZVR5cGU9IlhNTCIKCQkgICAgICAgIGF0dHJpYnV0ZU5hbWU9InRy YW5zZm9ybSIKCQkgICAgICAgIHR5cGU9InRyYW5zbGF0ZSIKCQkJCWZyb209 IjIwIiB0bz0iMCIKCQkJCWJlZ2luPSIwcyIgZHVyPSIwLjZzIgoJCQkJcmVw ZWF0Q291bnQ9ImluZGVmaW5pdGUiCgkJCQlhZGRpdGl2ZT0ic3VtIi8+CgkJ PC9yZWN0PgoJPC9nPgo8L3N2Zz4K
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

According to MDN docs, you might need to escape and then decodeURIComponent to handle unicode:

function utf8_to_b64( str ) {
    return window.btoa(unescape(encodeURIComponent( str )));
}

function b64_to_utf8( str ) {
    return decodeURIComponent(escape(window.atob( str )));
}

// Usage:
utf8_to_b64('? à la mode'); // "4pyTIMOgIGxhIG1vZGU="
b64_to_utf8('4pyTIMOgIGxhIG1vZGU='); // "? à la mode"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...