Thursday, November 4, 2010

UTF-8 characters get garbled in AJAX response

This problem is often experienced in websites dealing with multiple languages and special characters. Characters are unicode characters and there are many ways in which they can be encoded. UTF-8 is just one way of the ways of doing it.

Consider an AJAX request that fetches HTML text from a database:

xmlhttp.open("GET","get_html.php",true);

The response to this request could possibly contain special characters (entered through a WYSIWYG editor) which will get rendered as "junk" characters as there is no natural way for the response to know the encoding. As a result the output fetched by the response needs to be encoded. One of the ways of getting HTML text rendered correctly in an AJAX response is by encoding it using utf8_encode() and then using html_entity_decode() as follows:

//code to fetch response data from db into a var, say $db_data
$utf8_encoded_char = utf8_encode($db_data);
$html_text = html_entity_decode($utf8_encoded_char);

No comments:

Post a Comment