Characters with jquery json

I am using jquery $ .getJSON to get a list of cities.

Everything works fine, but I am from Estonia, and we use some characters, such as õ, ü. ä, ö.

When I pass such letters to the callback function, I keep getting blank lines. I tried to use base64 encode (server-side) -decode (jquery base64 plugin) strings (I thought it was a good idea while I can compress pages with php, so I don't need to worry about bandwidth), but this way I get some random chinese characters.

What will be the best solution to this problem.

Thanks.

+7
jquery base64 unicode getjson
source share
4 answers

You need utf8_encode() your output. There is no need to set client-side encoding in UTF-8, as it is already installed by 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> 
+4
source share

You must ensure that you maintain a consistent character encoding. The Estonian characters you want are in utf-8, so you need to use $.ajax and explicitly set the char encoding (via contentType).

You can take the resulting ajax and process it.

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

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

This will allow you to use getJSON

In addition, there is a corresponding SO publication on this subject: How to set the encoding in .getJSON jQuery

+2
source share

Use UTF-8 on your php pages and as output encoding

0
source share

This looks like an encoding / encoding problem: are you sure you are using:

  • Is the same encoding everywhere in your application?
    • Database
    • source files
    • HTTP headers sent to the browser
  • An encoding that supports these characters?
    • i.e. .: are you using UTF-8?
    • What is Unicode encoding, which is commonly used for a web application, and supports almost any character you might think of
0
source share

All Articles