Jquery-ajax Chinese garbled solution, jqueryajax garbled

Source: Internet
Author: User

Jquery-ajax Chinese garbled solution, jqueryajax garbled
$. Ajax ({
DataType: 'json', type: 'post', url: 'http: // localhost/test. do ', data: {id: 1, type: 'item'}, success: function (data ){}});
Problem:
When the background action program is submitted, the type obtained is garbled
Solution:
Method 1: encodeURI is used for encoding before submission. Remember that it must be twice.
1. Modify the following code:

Data: {id: 1, type: encodeURI ('item '))}

2. decode the obtained string in the background action.
1. String type = request. getParameter ("type ");
2. type = URLDecoder. decode (type, "UTF-8 ″);
Method 2: ajax configure the contentType attribute, add charset = UTF-8
Add the following parameters to the ajax method:
ContentType: "application/x-www-form-urlencoded; charset = UTF-8" use other js frameworks or xhr are similar, set the contentType in the header,
The key here is charset = UTF-8, if there is no such, it will not work, the default contentType in jQuery is not

I. Test Environment
JQuery: 1.3.2
Tomcat: 5.5.17
Ii. Test Methods
1. Use the get Method
Server-side java code:

String name = new String (request. getParameter ("name"). getBytes ("iso8859-1"), "UTF-8 ");

Client js Code:

$. Ajax ({url: "2.jsp", type:" get ", data: {name:" Chinese "}, success: function (response ){
Alert (response );
}});

Result: displayed correctly.

$. Ajax ({url: "2.jsp", type:" get ", data:" name = Chinese ", success: function (response ){
Alert (response );
}});

Result: Garbled

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

Result: displayed correctly.
Copy codeThe Code is as follows:
$. Get ("2.jsp"," name = Chinese ", function (response ){
Alert (response );
});

Result: Garbled

2. post Method
Server-side java code:

Request. setCharacterEncoding ("UTF-8 ");
String name = request. getParameter ("name ");

Client js Code:

$. Ajax ({url: "3.jsp", type:" post ", data:" method = testAjaxPost & name = Chinese ", success: function (response ){
Alert (response );
}});

Result: displayed correctly.
$. Ajax ({url: "3.jsp", type:" post ", data: {name:" Chinese "}, success: function (response ){
Alert (response );
}});

Result: displayed correctly.

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

Result: displayed correctly.

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

Result: displayed correctly.
3. Use filter

Public void doFilter (ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
If (req. getHeader ("X-Requested-")! = Null & req. getHeader ("X-Requested-With"). inclusignorecase ("XMLHttpRequest ")){
Request. setCharacterEncoding ("UTF-8 ");
} Else {
Request. setCharacterEncoding ("gbk ");
}
Chain. doFilter (request, response );
}

When using ajax, jQuery will add X-Requested-With to the header. The value is XMLHttpRequest. When the filter judges that it is a jQuery ajax request, it sets the character encoding to utf8, this solves the problem of garbled Chinese Characters in post submission, and does not need to set request in the code. setCharacterEncoding ("UTF-8 ");
For Chinese garbled characters in get mode, we recommend that you do not submit Chinese Characters in get mode, and change all to post ^-^.

To be consistent with the way prototype. js processes Chinese, you can use the following method to customize the attribute RequestType in the header.

$. 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:

Public void doFilter (ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
If (req. getHeader ("RequestType ")! = Null & req. getHeader ("RequestType"). inclusignorecase ("ajax "))){
Request. setCharacterEncoding ("UTF-8 ");
} Else {
Request. setCharacterEncoding ("gbk ");
}
Chain. doFilter (request, response );
}

JQuery ajax Chinese garbled

1. The front and back-end encoding should be unified;
2. When using gb2312 encoding, do not use $. get () or $. post () of jquery for ajax submission, because the two methods are UTF-8 by default;
3. Use $. add contentType: "pplication/x-www-form-urlencoded; charset = GB2312" to ajax () and write it in the following format to avoid garbled code in most cases:
$. Ajax ({
Type: "POST ",
ContentType: "pplication/x-www-form-urlencoded; charset = GB2312 ",
Url: "XXX",
Data :{},
Success: function (msg ){
Alert (msg );
}
});
 
Chinese garbled characters returned by AJAX methods in jquery? Solution

Asp or asp.net is used in the background. Try encoding Chinese characters!

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.