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

13 comments so far

  1. [...] Sul blog in inglese nelle ultime due settimane ho scritto alcuni post, in inglese, sulle problematiche dell’utilizzo di ajax con encoding che non sono UTF-8. [...]

  2. Tassoman on

    Which js function shall I use to decode the international text for the xmlHttprequest?

  3. fullo on

    unescape

  4. rizwan on

    I am also facing the same problem. I tried your way like this. However, on the server side I am using Java (servlet). Funny thing is that using escape(), my servlet cannot get the parameter (it is null). However if not using escape, my servlet can get it, but crap one.

  5. PATTAN on

    Thanks. This saved me hours of debuging.

  6. ja on

    thanks, helped a lot :)

  7. Binzy Wu on

    to rizwan,
    because in the request, the method getParamter including the decode process.
    so in my way, i just use the getQueryString, and by using regular express to prase it.
    hope it can help you.

  8. Thiago on

    Hi!

    I’m a webdesigner from Brazil and I really enjoyed your posts. They were very usefull and solved my doubts.

    Thank you!

  9. Aditya Mooley on

    Hi,

    Thanks a lot for this help.
    But the problem with urlencode is it is converting spaces to + and Javascript unescape is unable to convert it back to space.

    Any solution?

    Thanks

  10. Maicon Brauwers on

    Hi
    Aditya Mooley, use rawurlenconde function instead urlconde in your php script.

  11. Per Henning Johansen on

    Hi!

    Thanks a lot!! Searched the web for solutions, but yours is definitely the best one for me. I guess this problem will be solved in future versions of ajax, hence a simple solution is just what I wanted.

  12. Friðrik on

    Could you give some examples of it in action?

  13. baldo on

    fico.. mi mancava questo:

    urldecode


Lascia un Commento

Fill in your details below or click an icon to log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Log Out / Modifica )

Foto Twitter

You are commenting using your Twitter account. Log Out / Modifica )

Foto di Facebook

You are commenting using your Facebook account. Log Out / Modifica )

Connecting to %s

Iscriviti

Get every new post delivered to your Inbox.