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

Annunci

15 pensieri su “ajax encoding pt.2: from js->escape to php->urldecode

  1. 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.

  2. 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.

  3. 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.

  4. i’m using prototype.js for Ajax calls, and i had problem with Croatian language characters, when using ‘escape’ function.
    when i used ‘encodeURIComponent’ instead, it worked fine.

    hope this helps someone.

    (‘urldecode’ on php side worked fine with this)

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...