Characters with jquery json

base64, getjson, jquery, unicode

Solution

You need to `utf8_encode()` your output data. There's no need to set your encoding on the client side to UTF-8 as it's already default.

<?php
    if( isset( $_GET['json'] ) ) {

        die( json_encode( array( 'text' => utf8_encode( 'õ, ü. ä, ö' ) ) ) );
    }

    include_jquery(); # this outputs jquery include
>?

<script>

    $.getJSON( 'this_file.php', { 'json': 1 }, function( data ) {
        console.log( 'data', data );
    });

</script>

Problem

I'm using jquery $.getJSON to retrieve list of cities. Everything works fine, but I'm from Estonia and we are using some characters like õ, ü. ä, ö. When I pass letters like this to callback function, I keep getting empty strings. I've tried to base64 encode(server-side)-decode(jquery base64 plugin) strings (i thought it was a good idea as long as I can compress pages with php, so I don't have to worry about bandwidth), but in this way I end up with some random chinese symbols. What would be the best workaround for this problem. Thank you.

Original source

Related problems