How to encode/decode utf8 in ajax requests?
My page is encoded in iso-8859-1, but how can you easily encode the data to utf8 when sending and decode again when reveicing the response?
function Ajax(){
var _this = this;
this.url = null;
this.data = null;
this.success = null;
this.global = true;
this.timeout = JSON_TIMEOUT;
this.cache = false;
this.dataType = 'json';
this.type = 'post';
this.send = function(){
var jqxhr = $.ajax({
url : this.url,
data : this.data,
timeout : this.timeout,
cache : this.cache,
dataType : this.dataType,
type : this.type,
global : this.global
}
)
.success(this.success)
.error(function(){
// something
})
.complete(function(){
});
};
}
You don’t need to do anything. jQuery’s ajax() function will always transmit the data in UTF-8.
It doesn’t matter what your page is encoded in because Javascript always uses Unicode internally and properly translates your iso-8859-1 page.