Ajax中文乱码问题
2006-11-08 11:42
190 查看
产生原因
主要有2个原因
1 xtmlhttp 返回的数据默认的字符编码是utf-8,如果前台页面是gb2312或者其它编码数据就会产生乱码
2 post方法提交数据默认的字符编码是utf-8,如果后台是gb2312或其他编码数据就会产生乱码
解决方法
推荐方法,前台后台都用utf-8编码,这样可以省不少麻烦,从根本上解决了乱码问题.优点是效率高,而且符合目前的形式,utf-8编码本身就是一种比较优秀的编码,没有语言限制.缺点只能调用自己的后台编码或者其他的utf-8的编码.
程序代码
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
后台asp中第一行加入如下代码
<%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%>
<% Response.Charset="utf-8"; %>
<% Session.CodePage=65001; %>
后台如果有html代码也需保证
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
否则如果不用utf-8编码,就要在要读取的页面中设置:
ASP:Response.Charset="GB2312";
PHP:header('Content-Type:text/html;charset=GB2312');
JSP:response.setHeader("Charset","GB2312");
补救的方法是进行编码转换.可以在客户端也可以在服务器端进行编码转换.优点是没有任何限制,缺点效率低,例如:
<script language="VBScript" >
//解决中文显示问题
Function bytes2BSTR(vIn)
Dim strReturn,i,ThisCharCode,innerCode,Hight8,Low8,NextCharCode
strReturn = ""
For i = 1 To LenB(vIn)
ThisCharCode = AscB(MidB(vIn,i,1))
If ThisCharCode < &H80 Then
strReturn = strReturn & Chr(ThisCharCode)
Else
NextCharCode = AscB(MidB(vIn,i+1,1))
strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
i = i + 1
End If
Next
bytes2BSTR = strReturn
End Function
</script>
<script language="javascript">
function XHConn()
{
var xmlhttp, bComplete = false;
try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
catch (e) { try { xmlhttp = new XMLHttpRequest(); }
catch (e) { xmlhttp = false; }}}
if (!xmlhttp) return null;
this.connect = function(sURL, sMethod, sVars, fnDone)
{
if (!xmlhttp) return false;
bComplete = false;
sMethod = sMethod.toUpperCase();
try {
if (sMethod == "GET")
{
xmlhttp.open(sMethod, sURL+"?"+sVars, true);
sVars = "";
}
else
{
xmlhttp.open(sMethod, sURL, true);
xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
xmlhttp.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
}
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState == 4 && !bComplete)
{
bComplete = true;
fnDone(xmlhttp);
}};
xmlhttp.send(sVars);
}
catch(z) { return false; }
return true;
};
return this;
}
//回调函数
var fnWhenDone = function (oXML) {
document.write(bytes2BSTR(oXML.responseBody));
}
var myConn = new XHConn();
myConn.connect("http://www.163.com", "GET", "ct=0&ie=gb2312&bs=xmlhttp&sr=&z=&cl=3&f=8&wd=xmlhttp+%D6%D0%CE%C4", fnWhenDone);
</script>
主要有2个原因
1 xtmlhttp 返回的数据默认的字符编码是utf-8,如果前台页面是gb2312或者其它编码数据就会产生乱码
2 post方法提交数据默认的字符编码是utf-8,如果后台是gb2312或其他编码数据就会产生乱码
解决方法
推荐方法,前台后台都用utf-8编码,这样可以省不少麻烦,从根本上解决了乱码问题.优点是效率高,而且符合目前的形式,utf-8编码本身就是一种比较优秀的编码,没有语言限制.缺点只能调用自己的后台编码或者其他的utf-8的编码.
程序代码
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
后台asp中第一行加入如下代码
<%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%>
<% Response.Charset="utf-8"; %>
<% Session.CodePage=65001; %>
后台如果有html代码也需保证
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
否则如果不用utf-8编码,就要在要读取的页面中设置:
ASP:Response.Charset="GB2312";
PHP:header('Content-Type:text/html;charset=GB2312');
JSP:response.setHeader("Charset","GB2312");
补救的方法是进行编码转换.可以在客户端也可以在服务器端进行编码转换.优点是没有任何限制,缺点效率低,例如:
<script language="VBScript" >
//解决中文显示问题
Function bytes2BSTR(vIn)
Dim strReturn,i,ThisCharCode,innerCode,Hight8,Low8,NextCharCode
strReturn = ""
For i = 1 To LenB(vIn)
ThisCharCode = AscB(MidB(vIn,i,1))
If ThisCharCode < &H80 Then
strReturn = strReturn & Chr(ThisCharCode)
Else
NextCharCode = AscB(MidB(vIn,i+1,1))
strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
i = i + 1
End If
Next
bytes2BSTR = strReturn
End Function
</script>
<script language="javascript">
function XHConn()
{
var xmlhttp, bComplete = false;
try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
catch (e) { try { xmlhttp = new XMLHttpRequest(); }
catch (e) { xmlhttp = false; }}}
if (!xmlhttp) return null;
this.connect = function(sURL, sMethod, sVars, fnDone)
{
if (!xmlhttp) return false;
bComplete = false;
sMethod = sMethod.toUpperCase();
try {
if (sMethod == "GET")
{
xmlhttp.open(sMethod, sURL+"?"+sVars, true);
sVars = "";
}
else
{
xmlhttp.open(sMethod, sURL, true);
xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
xmlhttp.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
}
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState == 4 && !bComplete)
{
bComplete = true;
fnDone(xmlhttp);
}};
xmlhttp.send(sVars);
}
catch(z) { return false; }
return true;
};
return this;
}
//回调函数
var fnWhenDone = function (oXML) {
document.write(bytes2BSTR(oXML.responseBody));
}
var myConn = new XHConn();
myConn.connect("http://www.163.com", "GET", "ct=0&ie=gb2312&bs=xmlhttp&sr=&z=&cl=3&f=8&wd=xmlhttp+%D6%D0%CE%C4", fnWhenDone);
</script>
相关文章推荐
- php ajax 中文乱码问题解决办法(接收发送)utf-8 GBK GB2312
- ajax 使用post提交中文乱码问题
- struts+ajax中文参数乱码解决问题
- jsp中 ajax的get请求的中文乱码问题的解决方法
- Ajax使用POST提交中文乱码问题
- ajax参数传递时中文乱码问题
- ajax中文乱码问题的总结
- Ajax提交中文乱码问题
- PHP Ajax中文乱码问题解决方法
- Ajax取得XML响应的中文乱码问题(持续整理中...)
- jQuery load方法及Ajax传递中文参数值乱码问题解决
- 解决ajax请求中文参数乱码问题
- ajax中文乱码问题
- AJAX的中文乱码问题
- ajax请求中的中文数据乱码问题
- ajax的中文乱码问题
- 用ajax传递参数时遇到中文乱码问题的解决方法
- ajax 文本框输入提示中文乱码问题解决方案
- ajax返回中文乱码,servlet中的显示乱码,post提交的乱码问题,编码大总结
- Ajax中文乱码问题解决方案