1

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');
  }
})

;

webber
  • 1,834
  • 5
  • 24
  • 56
  • Finally, figured out why is was not woking when the string was not hardcoded. I hadn't set the charecter encodin of the document! Thanks evenyone! – webber Mar 07 '12 at 22:50

2 Answers2

1

Try encoding data value with encodeURIComponent()

data: encodeURIComponent('MÉXICO')

Source: JQuery AJAX is not sending UTF-8 to my server, only in IE

Community
  • 1
  • 1
arunes
  • 3,464
  • 2
  • 21
  • 31
  • He's using POST. So I don't think that pertains to him. – Daniel Moses Mar 07 '12 at 20:19
  • no luck! I even change the servlet code to decode the stringURLDecoder.decode( builder.toString(), "UTF-8" ); – webber Mar 07 '12 at 20:24
  • Maybe this helps http://stackoverflow.com/questions/607176/java-equivalent-to-javascripts-encodeuricomponent-that-produces-identical-outpu – arunes Mar 07 '12 at 20:27
1

I don't understand what are you doing and what kind of request parameter are you sending. Shouldnt code be something like that:

...
data:{key:'MÉXICO'},
...

OR

...
data:{key:encodeURIComponent('MÉXICO')},
...

AND in servlet

...
request.getParameter('key')
...

OR

 stringURLDecoder.decode(
                             request.getParameter('key').toString(),
                             'UTF-8'
                        );

Or I didn't understand anything (If this is the case sorry :/)

elrado
  • 4,960
  • 1
  • 17
  • 15
  • +1 even if this doesn't resolve what the OP asked, it is important to note that you need to send data in the format that you specified. In this case he said it would be `application/x-www-form-urlencoded` – Daniel Moses Mar 07 '12 at 20:57
  • I just tried both your suggestionns and they didn't work. I still get MÉXICO on the server. Thanks anyway! – webber Mar 07 '12 at 20:59
  • DMoses: what should i change the contentType to? – webber Mar 07 '12 at 21:01
  • It's fine as is if you always plan on encoding it. – Daniel Moses Mar 07 '12 at 21:04
  • Sorry I think I'm going down the wrong path by explaining the byte encoding at a protocol instead of keeping it all in the application space. I should have followed the rules of encapsulation. I deleted my answer as it pertains to the protocol level which would be important in a web service enviornment. – Daniel Moses Mar 07 '12 at 21:31
  • DMoses, any idea why posting with the hardcoded encoded string works but calling via encodeURIComponent doesnt? – webber Mar 07 '12 at 21:36