Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chartset encoding when using Ajax ? JQuery

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 ?

like image 436
Arthur Ronald Avatar asked Jan 05 '10 18:01

Arthur Ronald


People also ask

How does AJAX work in jQuery?

AJAX = Asynchronous JavaScript and XML. In short; AJAX is about loading data in the background and display it on the webpage, without reloading the whole page. Examples of applications using AJAX: Gmail, Google Maps, Youtube, and Facebook tabs. You can learn more about AJAX in our AJAX tutorial.

What is content type in AJAX?

The jQuery ajax contenttype option is a built-in option that is passed to the ajax() function in the jQuery. The contenttype option is also called as MIME (multipurpose internet mail extension) type, it includes an HTTP header that specifies the information about what kind of data we are sending to the server.


1 Answers

I resolve it in PHP with the following line:

foreach($_POST as $key => $value) {
    $_POST[$key] = utf8_decode($value);
}
like image 163
Jorge Burdz Avatar answered Nov 01 '22 07:11

Jorge Burdz