I have some javascript that sends data in JSON through POST format to a PHP script.
Everything works fine with "usual" characters, but I see inconsistencies when using, for example, vowels with accents such as "à". I would like to ask if anyone has suggestions on how to fix this.
This is the Javascript:
$.ajax({
contentType: 'application/json',
data: JSON.stringify({
"action": params.action,
"username": params.username,
"page": params.page,
}),
processData: false,
//dataType: 'json',
url: "/w/ImolaCustom/SudoAutoedit.php",
type: 'POST',
success: function(data) {
...
}
});
On the PHP side of things I do this:
$theData = json_decode(file_get_contents('php://input')), true);
The problem presents itself if I send something like:
params.page = "Società sportiva Bridge";
as $theData['page'] becomes "Societ\xc3\xa0 sportiva Bridge"
If I use utf8_decode($theData['page']) (or if I use it on the string passed from php://input before json_decoding it I get "Societ\xe0 sportiva Bridge" instead.
I tried different conversion functions like iconv(), mb_convert_variables() and mb_convert_encoding() to convert from UTF-8 to ISO-8859-1 with the same results as above.
I also tried encoding the string client-side with encodeURIComponent() or escape(). PHP receives the correct string (respectively "Societ%C3%A0%20sportiva%20Bridge" and "Societ%E0%20sportiva%20Bridge"), but after decoding it with rawurldecode() I still get "Societ\xc3\xa0 sportiva Bridge" and "Societ\xe0 sportiva Bridge" respectively.
Both files are on a CentOS machine and are saved with EOL Conversion in UNIX Mode and with Charset Encoding set to UTF-8 (editor is notepad++).