jquery Ajax through the value, the background of Chinese garbled, after some groping found that the original client browser through get way to the project backstage, the encoding format is iso-8859-1, we need to go through the background transcoding to normal use. Here's how:
str = new String (str.getbytes ("iso-8859-1"), "Utf-8");
It is important to note that if you test locally, the default character encoding for background parsing is gb2312, no transcoding is required, so the code is certified as:
if (! Stringutil.getencoding (str). Equalsignorecase ("GB2312")) {
str = New String (Str.getbytes ("iso-8859-1"), "Utf-8");
}
The Stringutil class is as follows:
package com.hwcampus.common;public final class stringutil {public static Boolean isempty (STRING STR) {if (Str != null && !str.trim (). IsEmpty ()) {// if the string is not null and the value is not equal to the empty string after the whitespace is removed, the proof string has a substantial content return false;//not null}return true;// Null}private stringutil () {}/** * Determine the encoding type of the string * @param str * @return * author htc */public static string getencoding (STRING STR) { String encode = "GB2312"; if (IsEmpty (str)) return ""; try { if (Str.equals ( New string (Str.getbytes (encode), encode)) { string s = encode; return s; } } catch (exception exception) { } encode = "ISO-8859-1"; try { if (Str.equals (new string (str.getbytes (encode), encode)) { string s1 = encode; return s1; } } catch (Exception exception1) { } encode = "UTF-8"; try { if (Str.equals (new string ( Str.getbytes (encode), encode)) { String s2 = encode; return s2; } } catch (EXception exception2) { } encode = "GBK"; try { if (Str.equals (new string (str.getbytes (encode), encode)) { String s3 = encode; return s3; } } catch (Exception exception3) { } return ""; } }
jquery Ajax pass value, get way background Chinese garbled