Solution for Ajax Post submission in JQuery to garbled Chinese Characters in IE

Source: Internet
Author: User

Introduction: In the Ajax POST request of JQuery, the request is made. The Chinese characters in the request are displayed as garbled characters in the background. How can this problem be solved?

Problem introduction:
Copy codeThe Code is as follows:
Var regid = $ ('# oregion'). combobox ('getvalue ');
// Var sname = $ ('# sname'). val ();
Var sname = encodeURI ($ ('# sname'). val (), "UTF-8 ");
If (regid! = "" & Regid! = 'Undefined '){
$. Ajax ({
Async: true,
Type: "POST ",
DataType: "json ",
Url: "<% = request. getContextPath () %>/secretary/isHasUser. do? Regid = "+ regid +" & sname = "+ sname,
Success: function (data ){
If (data. mes! = "" & Data. mes! = Null ){
$. Messager. alert ('warning', data. mes );
Return false;
}
}
});
}
}

The above code can pass Chinese characters normally in FF/Chrome, but there is a problem in IE, and the output information in the background is garbled.

Solution:

1. Set contentType: "text/plain; charset = UTF-8". the problem persists.

2. Check again the jsp page where the ajax code is located, all of which use UTF-8, but the problem still exists.

3. In the background java code, the UTF-8, gbk, and gb2312 encoding must be used. The problem persists.

After analysis, it is determined that it may be related to content transmission in the browser or JQuery, and cannot be determined accurately at present.

Solution:

Front end: encodeURI ($ (dom). val (), "UTF-8"); encode with encodeURI

Back-end Java code:

URLDecoder. decode (inputStr, "UTF-8"): the backend code decodes the string passed in the previous section.

Summary:

Finally, you can view the string information transmitted by the Ajax Post request to display Chinese characters normally.

Related Article

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.