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.