Encoding problem Servlet, json and jquery.ajax

Asked

Viewed 646 times

2

I created a Servlet q returns a simple JSON. I was able to consume it quietly, but by putting the content in a div was as follows:

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

Looking at the return of Servlet from the Chrome network tab I saw that JSON came with the encoding correct, follow the return:

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

I realized 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 (actually changed, because I already had), I put contentType and encoding in ajax and nothing.

Follow my code (already tried with 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++;
   });
});
  • Things I would try: 1. Open Veloper tools, open tab Network and save the ajax. See the content-type of the request if it is the same as you are passing. 2. goes in your editor, with the page Servet opened and goes in "save as" and chooses UTF-8. Only the meta-description encoding sometimes does not. If you have JS files you also save them as UTF. You would also test without these 1252 encodings on .ajax from JSON. It’s always easier to be all UTF.

2 answers

2

Have you tried using the content-type "text/html" or "application/json" ? As encoding uses UTF-8 or ISO same... Recalling that the content-type of your file which is . html or . php etc, which contains the <div> should be correct as well, since you commented that the return of JSON is correct, it may be this...

  • unfortunately it didn’t work

  • how is your html? and the return of the database, check if it is encoded in the same way..

  • this exactly as it was has not changed anything, the output remains the same

  • It is not database is fixed, I created for test so I didn’t use the database, I created in string manually

  • example: Object.put("Item", "water");

2

Dude, if you’re using application server try this:

Reference: http://balusc.omnifaces.org/2009/05/unicode-how-to-get-characters-right.html

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

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

Glassfish 3.0(adds line down in the /WEB-INF/sun-web.xml) or Glassfish 3.1 file (adds line down in the /WEB-INF/web.xml file)

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

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

  • Thank you very much friend!!! worked here, I had to add: Resp.setCharacterEncoding("UTF-8"); req.setCharacterEncoding("UTF-8"); in Servlet q ajax consumes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.