I'm trying to POSt a UTF-8 string to my servlet (hosted in TomCat) but the string read on the server side is not UTF-8. I've verified via firebug that the ajax header's content-type has the utf-8 encoding set. I'm posting "MÉXICO" to the server but I'm receiving "MÉXICO".
$.ajaxSetup({
scriptCharset: "utf-8" ,
contentType: "application/x-www-form-urlencoded; charset=UTF-8"
});
$.ajax({
url: "SetData",
type: 'POST',
data: 'MÉXICO',
success: function(){
console.log('Data set');
}
});
Here's the java code in the servlet
request.setCharacterEncoding("UTF-8");
BufferedReader reader = new BufferedReader( new InputStreamReader( request, "UTF-8" ) );
char[] chr = new char[5];
reader.read( chr );
builder.append( chr );
UPDATE 1
I changed the posting data in js to encodeURIComponent('MÉXICO') but the server still reads "MÉXICO".
But, if I get the string value of encodeURIComponent('MÉXICO') i.e. 'M%C3%89XICO' it works! I'm zapped! Any idea why this should work?
$.ajax({
url: "SetData",
type: 'POST',
//data: 'MÉXICO', ** does not work
//data: encodeURIComponent('MÉXICO'), ** does not work
data: 'M%C3%89XICO', //works !!!!
success: function(){
console.log('Data set');
}
})
;