Archivio per la categoria ‘javascript’
ajax encoding pt.2: from js->escape to php->urldecode
Another brick to solve the encoding issue with ajax is settled, last week I ranted about the problem on showing data from a remote script inside a page encoded with ISO-8859-1.
This time I’ve the reverse problem, I’ve to save some data from a form to a db and then show it on another page. As usual xmlHttpRequest send data in UTF-8, so my data results always corrupted.
This time the solution is very simple, first I encode the data with javascript using the escape function then on the remote script I use the php function urldecode.
So a string like ÎÑTËRÑÅTÌÔñ�L will be escaped to %CE%D1T%CBR%D1%C5T%CC%D4%F1%C1L and then urldecoded back to ÎÑTËRÑÅTÌÔñ�L
ciuaz
utf-8 madness
As I wrote, I’m working on a stupid ajax (intranet) site but I’ve a lot of problem with charset encoding.
I’ve to publish, for legacy problem, all the intranet site using old ISO-8859-1 encoding BUT using the js XMLHttpRequest the data I receive is always UTF-8.
Unlucky I cannot use php to re-parse the data and to trasform to a latin encoding, anyone has a good idea to resolve this? Can I force another encoding on XMLHttpRequest?
Ok, I’m too tired to see the obvious… To resolve the problem I had simply forced the encoding via the php header function in the file I call remotely:
header(“Content-Type: application/x-javascript; charset=ISO-8859-1″);
echo $output;
now the browser show the right ÎÑTËRÑÅTÌÔñ�L strings
ciuaz
Commenti (13)
Commenti (6)

