如何解决XmlHttp.responseText的乱码问题

<iframe marginwidth="0" marginheight="0" src="http://218.16.120.35:65001/PC/Global/images/b.html" frameborder="0" width="728" scrolling="no" height="90"></iframe>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<title>如何解决XmlHttp.responseText的乱码问题</title>
<script type="text/vbscript">
Function URLEncoding(vstrIn)
strReturn = ""
For i = 1 To Len(vstrIn)
ThisChr = Mid(vStrIn,i,1)
If Abs(Asc(ThisChr)) < &HFF Then
strReturn = strReturn & ThisChr
Else
innerCode = Asc(ThisChr)
If innerCode < 0 Then
innerCode = innerCode + &H10000
End If
Hight8 = (innerCode And &HFF00)/ &HFF
Low8 = innerCode And &HFF
strReturn = strReturn & "%" & Hex(Hight8) & "%" & Hex(Low8)
End If
Next
URLEncoding = strReturn
End Function

Function bytes2BSTR(vIn)
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 type="text/javascript">
function Test()
{
var http = GetXmlHttp();
var url;
url = "http://www.baidu.com/s?tn=cjlgainmoney&ct=&lm=&z=&rn=&word=%CA%AF%BE%B8&_sv=1&_si=%CB%D1%CB%F7";
// url = "http://www.baidu.com/s?tn=cjlgainmoney&word=%CA%AF%BE%B8&_sv=1&_si=%CB%D1%CB%F7";
http.open("GET", url, true);
http.onreadystatechange = function()
{
if (http.readyState == 4 && http.status ==200)
{
var text;

//乱码处理,主要是用vbscript的bytes2BSTR
//看到主题<title>和</title>之间是【百度搜索_石靖】了。
text = bytes2BSTR(http.responseBody);

// text = http.responseText;//直接用http.responseText会显示乱码

alert(text);//看到主题<title></title>之间是乱码


// document.getElementById("HtmlDiv").innerText = text;
}
}
http.send(null);
}

function GetXmlHttp()
{
var C = null;
try
{
C = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
C = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(sc)
{
C = null;
}
}

if( !C && typeof XMLHttpRequest != "undefined" )
{
C = new XMLHttpRequest();
}

return C;
}
</script>
</head><body>
<button οnclick="Test()">请单击测试乱码</button>

<div id="HtmlDiv">
</div>
</body></html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值