JQuery asynchronous retrieval of the returned value of Chinese garbled solution, jquery Return Value

Source: Internet
Author: User

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 ();}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.