A variety of solutions for jquery Chinese garbled _jquery

Source: Internet
Author: User

1, the use of $.ajax appear in the Chinese garbled solution:

Copy Code code as follows:

var _realname = $ ("input[name= ' _searchname ')"). Val ();
var Termcourseid = ' <%=termid%> ';
var classId = ' <%=classid%> ';
var url = "/addressbook/studentlistnopage.do";
var dataurl = "formmap.termcourse_id=" +termcourseid+ "&formmap.class_id=" +classid+ "&formmap.is_online=" All&formmap.realname= "+_realname;
$.ajax ({
Type: "POST",
Url:url,
DataType: "JSON",
Data: {
"formmap.termcourse_id": Termcourseid,
"formmap.class_id": ClassId,
"Formmap.is_online": "All",
"Formmap.realname": encodeURI (_realname)
},
ContentType: "application/x-www-form-urlencoded; Charset=utf-8 ",
Success:function (data) {
data = eval (data);
var html = "";
$ ("#allUnselectedUser"). HTML (HTML);
},
Error:function (XMLHttpRequest, Textstatus, Errorthrown) {
alert (textstatus);
}
});

When the use of Dataurl & in the submission, whether the foreground is using encodeURI or encodeuricomponent or escape the Chinese transcoding, submitted to the action are garbled, not want to%e6%b1%89%e5% ad%97 this transcoding code. Even if you add contenttype.

Submit the way of & in Dataurl to Data:{name:value}.

Use Urldecoder.decode (realname, "UTF-8") in the action to convert the code to Chinese. The use of UTF-8 is because the default method of jquery submission is UTF-8, even if the charset in ContentType modify other, such as GBK, the background also UTF-8 modify GBK, can not correct conversion.

JQuery Ajax garbled Problem solving
First, test environment
jquery:1.3.2
tomcat:5.5.17
Second, test methods
1. Using Get method
Server-side Java code:

Copy Code code as follows:

String name = new String (request.getparameter ("name"). GetBytes ("Iso8859-1"), "Utf-8");

Client JS Code:
Copy Code code as follows:

$.ajax {url: ' 2.jsp ', type: ' Get ', data: {name: ' Chinese '},success:function (response) {
alert (response);
}});

Results: Correct display
Copy Code code as follows:

$.ajax ({url: "2.jsp", type: "Get", Data: "Name= Chinese", success:function (response) {
alert (response);
}});

Result: garbled
Copy Code code as follows:

$.get ("2.jsp", {name: "Chinese"},function (response) {
alert (response);
});

Results: Correct display
Copy Code code as follows:

$.get ("2.jsp", "name= Chinese", function (response) {
alert (response);
});

Result: garbled

2.post mode
Server-side Java code:

Copy Code code as follows:

Request.setcharacterencoding ("UTF-8");
String name = Request.getparameter ("name");

Client JS Code:
Copy Code code as follows:

$.ajax ({url: "3.jsp", type: "POST", Data: "Method=testajaxpost&name= Chinese", success:function (response) {
alert (response);
}});

Results: Correct display
Copy Code code as follows:

$.ajax ({url: "3.jsp", type: "POST", data: {name: "Chinese"},success:function (response) {
alert (response);
}});

Results: Correct display
Copy Code code as follows:

$.post ("3.jsp", {name: "Chinese"},function (response) {
alert (response);
});

Results: Correct display
Copy Code code as follows:

$.post ("3.jsp", "name= Chinese", function (response) {
alert (response);
});

Results: Correct display
Third, the use of filter
Copy Code code as follows:

public void Dofilter (ServletRequest request, servletresponse response,
Filterchain chain) throws IOException, Servletexception {
HttpServletRequest req = (httpservletrequest) request;
if (Req.getheader ("X-requested-with")!= null && req.getheader ("X-requested-with"). Equalsignorecase (" XMLHttpRequest ")) {
Request.setcharacterencoding ("Utf-8");
} else {
Request.setcharacterencoding ("GBK");
}
Chain.dofilter (request, response);
}

jquery adds x-requested-with to the header when using AJAX, with the value: Xmlhttprequest,filter the character encoding to UTF8 when the AJAX request in jquery is judged to be This solves the Chinese garbled problem in post submission, and does not need to set request.setcharacterencoding ("UTF-8") in the Code;
For the Chinese garbled problem of get way, we suggest not to use get method to submit Chinese, all change to post ^-^

To be consistent with the way Prototype.js handles Chinese, you can customize the properties in the header by using the following methods RequestType
Copy Code code as follows:

$.ajax ({
URL: "3.jsp",
Type: "Post",
Data: {name: "Chinese"},
Beforesend:function (XMLHttpRequest) {
Xmlhttprequest.setrequestheader ("RequestType", "Ajax");
Alert ("Start");
},
Success:function (data, textstatus) {
alert (data);
},
Error:function (XMLHttpRequest, Textstatus, Errorthrown) {
Alert ("Error:" + textstatus);
},
Complete:function (XMLHttpRequest, Textstatus) {
Alert ("Finished:" + textstatus);
}
});

The filter code is as follows:
Copy Code code as follows:

public void Dofilter (ServletRequest request, servletresponse response,
Filterchain chain) throws IOException, Servletexception {
HttpServletRequest req = (httpservletrequest) request;
if (Req.getheader ("RequestType")!= null && req.getheader ("RequestType"). Equalsignorecase ("Ajax")) {
Request.setcharacterencoding ("Utf-8");
} else {
Request.setcharacterencoding ("GBK");
}
Chain.dofilter (request, response);
}

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.