views:

532

answers:

3

Hi,

I have a web application (UTF-8) in which the following one can be used to send to the server side

áéíóú
àèìòù
ÀÈÌÒÙ
ÁÉÍÓÚ

Ok. I use something like as follows to send data

// Notice $("#myForm").serialize()
$.get("/path?", $("#myForm").serialize(), function(response) {

});

When i see my recordSet, i get (database charSet encoding is UTF-8)

áéíóú
à èìòù
ÃÉÃÓÚ
ÀÈÌÒÙ

Even when using $.post, i get the same result set

After seeing serialize() method in JQuery in Action book:

Creates a properly formatted and encoded query string from all successful form elements in the wrapped set

But, as shown above, it does not appear to work fine. So instead of serialize() method, i use

var objectArray =  $("#myForm").serializeArray();

var queryString = "";
for(var i = 0; i < objectArray.length; i++) {
    queryString += "&" + objectArray[i]["name"] + "=" + objectArray[i]["value"];
}

$.get("/path?" + queryString, null, function(response) {

});

Now i get in database

áéíóú
àèìòù
ÀÈÌÒÙ
ÁÉÍÓÚ

So Am i missing something when using serialize() method ? Why serialize() method does not work as expected ?

+2  A: 

"jQuery.get" like all the functions jQuery ajax use "encodeURIComponent" for the data. If you send data via GET, the XHR automatically takes care of passing the encoding of URLs. You can view the documentation of "encode" to see which type of encoding is suitable for the data you send

andres descalzo
A: 

You should add the following meta to the head of both the initiating and recieving page:

meta http-equiv="Content-type" content="text/html; charset=utf-8"

This will default the page to render content in utf-8. If this alone doesn't work you can use encodeURIComponent() in addition to encode the characters when sending. If need be on the receiving end if the encoding isn't automatically decoded, run decodeURIComponent().

CogitoErgoSum
A: 

Thank you very much - that article saved my project :-) Thanks a lot

michal