Hello. I am interested in how to solve the problem of displaying question marks instead of Cyrillic characters in the JSON received by an Ajax request. Almost all the topics found using the search advise you to set a code page, but in my case it will not help in any way since the ad of the code page I have from the very beginning, along with the ad mime-type. Debager also showed that the server is transmitting normal JSON.

PrintWriter outs = response.getWriter(); SprIzmDao dao = new SprIzmDao(); int i = Integer.parseInt(request.getParameter("id")); EdIzmBean bean=dao.getIzmObj(i); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); outs.print(bean.toString()); outs.flush(); 

As you can see, the code is very simple. Perhaps the error in it lies in plain sight, but for some reason I don’t see it in emphasis.

  • How do I understand the error in the encoding? You instead of the text displayed kryakozyabry? - E1mir
  • Question marks with any encoding on the browser page. - Vadim Korzhenko
  • Hmmm, strange and interestingly, the code is quite simple .. I think the error lies in the classes SprIzmDao or EdIzmBean .. - E1mir
  • Debager showed that responsibly valid JSON is leaving. means that dao takes valid data from the base and forms a valid bin, and the base in UTF-8. - Vadim Korzhenko
  • library means curve - Drakonoved

1 answer 1

As it turned out, the problem was that the character encoding is set after creating the output stream.

 response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); PrintWriter outs = response.getWriter(); SprIzmDao dao = new SprIzmDao(); int i = Integer.parseInt(request.getParameter("id")); EdIzmBean bean=dao.getIzmObj(i); outs.print(bean.toString()); outs.flush();