Skip to content
This repository has been archived by the owner on Apr 27, 2024. It is now read-only.

Does not work with UTF chars #1

Open
kolarski opened this issue Sep 9, 2012 · 7 comments
Open

Does not work with UTF chars #1

kolarski opened this issue Sep 9, 2012 · 7 comments

Comments

@kolarski
Copy link

kolarski commented Sep 9, 2012

Does not work with UTF chars. Returns INVALID_CHARACTER_ERR: DOM Exception 5.

 function _getbyte(s, i) {
    var x = s.charCodeAt(i);
    if (x > 255) {
        throw "INVALID_CHARACTER_ERR: DOM Exception 5"
    }
    return x
}

I think that this should be mentioned somewhere.

@josefig
Copy link

josefig commented Oct 9, 2012

Oh, this is a shame! I was going to use for utf and I got the same error

@zrajm
Copy link

zrajm commented Nov 11, 2012

I wish I'd seen this earlier, it would've saved me some trouble. :(

I wound up using the suggestions in the following stackoverflow question.
http://stackoverflow.com/questions/246801/how-can-you-encode-to-base64-using-javascript

(Since there is a whole source code + multiple suggestions on how to implement base64 in javascript there, I simply cut-and-pasted to get all of the goodies in one file.)

@vote539
Copy link

vote539 commented Jun 25, 2014

+1

@born2net
Copy link

+1 to fix

@tmorehouse
Copy link

simple workaround:

    var b64str = $.base64.encode(unescape(encodeURIComponent(string_to_encode)))

This will breakup UTF8 chars into their component bytes and then pass to to the base 64 encode routine. unescape is not UTF8 aware to it treats each % encoded byte as a single character.

window.atob() requires the same workaround.

Although unescape is deprecated, and may start to disappear from modern browsers (ref https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/unescape)

@wieczorek1990
Copy link

@tmorehouse Thanks!

@fynman
Copy link

fynman commented Nov 15, 2016

for asp.net developers, return your JSON as:
return Json(model, "application/json" ,System.Text.Encoding.ASCII, JsonRequestBehavior.AllowGet);

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants