最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

PHP base64 encoded response does not get decoded correctly in Javascript - Stack Overflow

programmeradmin2浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 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/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

发布评论

评论列表(0)

  1. 暂无评论