views:

357

answers:

4

Hi everyone, I'm using jquery $.getJSON to retrieve list of cities.

Everything works fine, but I'm from Estonia (probably most of you don't know much about this country =D) 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.

A: 

Use UTF-8 in you php pages and as output encoding

jitter
A: 

This looks like some encoding/charset problem : are you sure you are using :

  • The same charset everywhere in your application ?
    • database
    • source files
    • HTTP headers sent to the browser
  • A charset that supports those characters ?
    • i.e. : are you using UTF-8 ?
    • Which is the Unicode charset that's generally used for web-application, and supports virtually any character one can think about
Pascal MARTIN
+1  A: 

You have to make sure you keep a consistant character encoding. The Estonian chars you want are in utf-8 so you have to use $.ajax and set the char encoding explicitly (via contentType).

You can take the resulting ajax and process it.

You could also use ajaxSetup before calling getJSON to set the content type.

http://api.jquery.com/jQuery.ajaxSetup/

This would allow you to use getJSON

Also, here is a relevant SO post about the same subject: http://stackoverflow.com/questions/26620/how-to-set-encoding-in-getjson-jquery

Joshua Smith
+3  A: 

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>
Andy
Looping array's with utf8_encode() before echoing json_encode solved the problem.Thank you for your hints everyone.
Mikk