Problem of encoding servlet, json and jquery.ajax

2

I created a servlet that returns a simple JSON. I was able to consume the same quietly, however putting the contents in a div was as follows:

0: �gua
1: a�ucar
2: sal
3: canela
4: �leo

When looking at the return of the servlet through the Chrome network tab, I saw that JSON came with the correct encoding , follow the return:

[{Item: "água"}, {Item: "açucar"}, {Item: "sal"}, {Item: "canela"}, {Item: "óleo"}]

I noticed that the problem is only in ajax, so how do I solve this problem?

I tried several solutions and nothing I found on the net solved, I put meta tag (I actually changed it, because I already had it), I put contentType and encoding in ajax and nothing.

Here's my code (I've already tried UTF-8, ISO, and finally Windows):

$.ajax({
   url: '',
   data: 'POST',
   dataType: 'json',
   encoding:"Windows-1252",
   contentType: "text/plain; charset=Windows-1252"
}).done(function(retorno){
   alert(retorno);
   var k = 0;
   $.each(retorno, function(i, item){
     criarElemento("<p>", {html: i+": "+item.Item, id: 'item_'+k}, "teste");
     k++;
   });
});
    
asked by anonymous 15.11.2016 / 23:28

2 answers

2

Ever tried to use content-type "text/html" or "application/json" ? As encoding uses UTF-8 or even ISO ... Remember that the content-type of your file that is .html or .php etc, which contains the <div> should be correct too, since you commented that the return of JSON is correct, this may be ...

    
16.11.2016 / 00:17
2

Dude, if using application server try this:

Reference: link

Tomcat (adds aligns below in file /conf/server.xml)

<Connector (...) URIEncoding="UTF-8" />

Glassfish 3.0 (add aligns below in file /WEB-INF/sun-web.xml) or Glassfish 3.1 (add aligns below in file /WEB-INF/web.xml)

<parameter-encoding default-charset="UTF-8" />

I had this problem with JSF in Glassfish, nothing solved until I discovered this solution ...

    
16.11.2016 / 14:39