1

I have a function that convert my inputs to base64 encode:

function goToMap()
{

 $rows = $('.address');
        $length_rows = $('.address').length;

        $arr_objs = Array();
        $rows.each( function(){

            $obj = {};
            $row_id = $(this).attr('data-id');

            $obj.id = $row_id;
            $obj.address  = $('#address_'+$row_id).val();
            $obj.type     = $('#type_'+$row_id).val();

            $obj.lat      = $(this).attr('data-lat')
            $obj.lnt      = $(this).attr('data-lnt');


            $arr_objs.push($obj);

        });

      $b4 = btoa($arr_objs);

      location.href = 'http:/****/?b4='+$b4;

}

but when I convert my inputs to base64 with btoa function, I can't convert it to pure json with php:

my base64 output:

W29iamVjdCBPYmplY3RdLFtvYmplY3QgT2JqZWN0XSxbb2JqZWN0IE9iamVjdF0=

after convert with php:

$all_address = json_decode(base64_decode($gets['b4']),true);
var_dump($all_address);
return;

output:

string(47) "[object Object],[object Object],[object Object]" 

my json output of my function:

[
  {
    "id": "35",
    "address": "تهران  خیابان امام خمینی",
    "type": "1",
    "lat": "35.6886846123666",
    "lnt": "51.387248933315284"
  },
  {
    "id": "91",
    "address": "تهران  خیابان امام خمینی",
    "type": "1",
    "lat": "",
    "lnt": ""
  },
  {
    "id": "36",
    "address": "کرج  کوچه جعفری",
    "type": "2",
    "lat": "35.84001878731047",
    "lnt": "50.93909069895744"
  }
]
F.Joodaki
  • 141
  • 3
  • 16

1 Answers1

2

I solved my problem with this fucntion:

function b64EncodeUnicode(str) {
    return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
        return String.fromCharCode(parseInt(p1, 16))
    }))
}

Using Javascript's atob to decode base64 doesn't properly decode utf-8 strings

F.Joodaki
  • 141
  • 3
  • 16