Skip to content Skip to sidebar Skip to footer

PHP Base64 Encoded Response Does Not Get Decoded Correctly In Javascript

Good day! I wonder why I cannot decode base64 response in javascript using atob or btoa or custom formulas like $.base64.decode(). This is what I have in php $res = 'Это тес�

Solution 1:

Sounds like a character encoding issue on your page.

Make sure you have the correct encoding type specified in a meta tag in your HTML head.

http://www.w3.org/International/questions/qa-html-encoding-declarations


Solution 2:

I found an answer to the question. It seems that atob and btoa badly handle UTF-8 encoding.

It is described in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Base64_encoding_and_decoding Solution 2 from section The "Unicode Problem" worked well for me.

Solution #2 – rewriting atob() and btoa() using TypedArrays and UTF-8


Post a Comment for "PHP Base64 Encoded Response Does Not Get Decoded Correctly In Javascript"