Yesterday I had a very strange situation… Using a jQuery datatable, I was trying to display some info, populating the table with Ajax calls… Everything was OK, until somebody didn’t try to use special characters, like “ö” or ENTER… Than the datatable went crazy… Here is what I found out:
Basically, with Ajax I was calling a php file, which will return the result of a query, let’s say it’s somebody’s name now. If that name contains special characters, first you have to call the following function in the php file:
function fix_str($str){
$str = htmlentities($str, ENT_COMPAT, ‘UTF-8′);
$str = str_replace(chr(10),”,$str);
$str = str_replace(chr(13),’
’,$str);
$str = rawurlencode($str);
return $str;
}
This guarantees that the result sent back to the jQuery won’t be messed up…
On the Javascript side, to get the result well formatted, if you have the Ajax call result in the variable “resp”:
str = unescape(resp);
This way you will be able to pass any character through the Ajax call.