The solution to Chinese Garbled text is actually very simple. before the transfer, encode and decode. See the source code snippet:
Servlet
/** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubrequest.setCharacterEncoding("UTF-8");PrintWriter pw = response.getWriter();String userId = request.getParameter("userId");String vcard = VcardHandle.queryVcard(userId);pw.print(URLEncoder.encode(vcard, "UTF-8"));pw.close();}
Javascript
$.ajax({type : "POST",contentType : "application/x-www-form-urlencoded; charset=utf-8",async : false,url : "VcardServlet?userId=" + userId,dataType : 'text',success : function(result) {vcard = decodeURIComponent(result);}});
In JavaScript, encodeuri does not encode the following characters: ":", "/", ";", and "?". We recommend that you use encodeuricomponent.
Dml@2013.1.8