jquery ajax传值,get方式后台中文乱码

经过jquery ajax传值,后台出现中文乱码,通过一番摸索后发现原来客户端浏览器经过get方式传递到项目后台时,编码格式是ISO-8859-1,须要咱们在后台通过转码才能正常使用。方法以下:java

str = new String(str.getBytes("ISO-8859-1"), "utf-8");jquery

须要注意的是,若是在本地测试,后台解析的默认字符编码是gb2312,则不需转码,所以代码修证为:ajax

if(!StringUtil.getEncoding(str).equalsIgnoreCase("GB2312")){浏览器

str = new String(str.getBytes("ISO-8859-1"), "utf-8");测试

}编码

StringUtil类以下:spa

package com.hwcampus.common;

public final class StringUtil {
	public static boolean isEmpty(String str) {
		if (str != null && !str.trim().isEmpty()) {// 若是字符串不为null,去除空格后值不与空字符串相等的话,证实字符串有实质性的内容
			return false;//不为空
		}
		return true;//为空
	}

	private StringUtil() {

	}

/**
 * 判断字符串的编码类型
 * @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 "";      
}  


}
相关文章
相关标签/搜索