admin管理员组文章数量:1405732
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 = "Это тескт";
echo base64_decode($res);
This is what I have in jquery
$.ajax({
type: "GET",
url: "request.php",
success: function(data){
$('#elementid').html($.base64.decode(data));
//or $('#elementid').html(atob(data)); // it gives the same result as the above one.
On the page I see the following
ÐÑо ÑеÑкÑ
Base64 encoded data looks the following way
0K3RgtC+INGC0LXRgdC60YI=
If I try to decode using different web tools from BASE64 to UTF-8 I see correct result, but not on my page using the above functions. Please suggest.
UPDATE: @logic-unit thanks for the suggestion. I forgot to mention that the resulting page index.php has the following in the heading
<?php
header('Content-Type: text/html; charset=utf-8');
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
...
</head>
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 = "Это тескт";
echo base64_decode($res);
This is what I have in jquery
$.ajax({
type: "GET",
url: "request.php",
success: function(data){
$('#elementid').html($.base64.decode(data));
//or $('#elementid').html(atob(data)); // it gives the same result as the above one.
On the page I see the following
ÐÑо ÑеÑкÑ
Base64 encoded data looks the following way
0K3RgtC+INGC0LXRgdC60YI=
If I try to decode using different web tools from BASE64 to UTF-8 I see correct result, but not on my page using the above functions. Please suggest.
UPDATE: @logic-unit thanks for the suggestion. I forgot to mention that the resulting page index.php has the following in the heading
<?php
header('Content-Type: text/html; charset=utf-8');
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
...
</head>
Share
edited Oct 3, 2013 at 18:38
KirillG
asked Oct 3, 2013 at 14:39
KirillGKirillG
691 gold badge1 silver badge5 bronze badges
2
- And your html page meta, looks like this: <meta charset="utf-8"> ?? – Hackerman Commented Oct 3, 2013 at 14:43
- atob/btoa doesn't like special characters, but i don't think having the correct charset helps... – dandavis Commented Oct 3, 2013 at 15:17
2 Answers
Reset to default 2I found an answer to the question. It seems that atob and btoa badly handle UTF-8 encoding.
It is described in https://developer.mozilla/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
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/International/questions/qa-html-encoding-declarations
本文标签: PHP base64 encoded response does not get decoded correctly in JavascriptStack Overflow
版权声明:本文标题:PHP base64 encoded response does not get decoded correctly in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744932936a2633008.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论