利用ajax 验证表单中用户名是否存在时输入中文字符不能验证,但是输入英文字符时可以,是什么原因?

来源:百度知道 编辑:UC知道 时间:2024/05/28 04:39:29
尽量详细解释,谢谢

乱码问题

Ajax获取gb2312编码的中文网页时,会出现乱码是因为JS存在数组中的数据是以UTF-8国际标准格式存储的,而中文一般是以GB2312格式存取的。UTF-8以三个字节显示一个汉字,而GB2312会以两个字节显示一个汉字

服务器端解决方案:
public static String UnicodeToGB(String strIn)
{
String strOut = null;
if(strIn == null || (strIn.trim()).equals(""))
{
return strIn;
}
try
{
byte[] b = strIn.getBytes("ISO8859_1");
strOut = new String(b, "GBK");
}
catch(Exception e)
{}
return strOut;
} //end GBToUnicode