JQuery asynchronous retrieval of the returned value of Chinese garbled solution, jquery Return Value
If you encounter a problem when using jqgrid to asynchronously retrieve the list value, the data obtained from the database on the server does not contain Chinese garbled characters (logs are not garbled ), however, garbled characters occur during asynchronous transmission to the customer. The server has been coded (UTF-8 encoding ). I started to suspect that it was a client problem. For example, the client and server encoding were inconsistent. I also suspected that the jqGrid tool function was missing the contentType: "application/x-www-form-urlencoded; charset = UTF-8 ", and so on.
None of the results. After several hours, I found that the original code was still on the server side and neglected.
@ RequestMapping (value = "/searchUserList. form ") @ ResponseBody public void searchUserList (int page, int rows, HttpServletRequest request, HttpServletResponse response) throws IOException {System. out. println ("idcard =" + idCard + "\ n page =" + page + "\ n rows =" + rows); List <User> list = userService. findByIDCard (idCard); int totalRecord = list. size (); int totalPage = totalRecord % rows = 0? TotalRecord/rows: (totalRecord/rows + 1); int index = (page-1) * rows; int pageSize = rows; String json = "{\" total \": \ "" + totalPage + "\", \ "page \": \ "" + page + "\", \ "records \": \ "" + totalRecord + "\", \ "rows \": ["; for (int I = index; I <pageSize + index & I <totalRecord; I ++) {User u = list. get (I); json + = "{\" id \ ": \" "+ u. getUserId () + "\", \ "userName \": \ "" + u. getUserName () + "\", \ "idCard \": \ "" + u. getIdCa Rd () + "\", \ "userTel \": \ "" + u. getUserTel () + "\", \ "userSex \": \ "" + u. getUserSex () + "\", \ "bankCard \": \ "" + u. getBankCard () + "\", \ "cardStatus \": \ "" + u. getCardSatus () + "\", \ "createTime \": \ "" + u. getCreateTime () + "\"} "; if (I! = PageSize + index-1 & I! = TotalRecord-1) {json + = "," ;}} json + = "]}"; request. setCharacterEncoding ("UTF-8"); // if this parameter is not set, the encoding will contain garbled response. setContentType ("text/html; charset = UTF-8"); response. setHeader ("Cache-Control", "no-cache"); PrintWriter out = response. getWriter (); // output Chinese characters. This sentence must be placed in response. setContentType ("text/html; charset = UTF-8"), response. setHeader ("Cache-Control", "no-cache"). Otherwise, the returned Chinese characters are garbled out. print (json. toString (); out. flush (); out. close ();}