更多相关内容...>>JavaScript中对XMLRequest的responseBody进行解码
JavaScript中对XMLRequest的responseBody进行解码
前面一篇日记学习了XMLHTTP的基本用法,用responseText其获得网页的内容如有Unicode字符的时候,则会显示乱码,
ZIVipm http://www.numino.net

1bgg77 http://www.numino.net
咋处理呢?这个时候还有另一个对象responseBody,看下它的运行结果:
e44q18 http://www.numino.net

MqVMwV http://www.numino.net
这个结果……更不像话,原因是因为responseBody存储的是字节,如果用vbsEdit来调试VBS版的XMLHTTP的话,则可以在DEBUG界面看到responseBody中存储的一个个字节形字数据,问题就出在这里了,在VBS中,可以用midb,ascb等手段将字节正确地解码,方式如下:
ZPjHLz http://www.numino.net
Set xml=CreateObject("msxml2.xmlhttp")
08C69Q http://www.numino.net
xml.open "get",url,False
xmsXmW http://www.numino.net
xml.send
Hr71cS http://www.numino.net
If xml.readyState=4 And xml.status=200 Then
i94pxk http://www.numino.net
'WScript.Echo lenb(xml.responseBody)
11Y25B http://www.numino.net
For i=1 To lenb(xml.responseBody)
YDPShF http://www.numino.net
curCode=ascb(midb(xml.responseBody,i,1))
4EHV2O http://www.numino.net
If curCode<&h80 Then
N7HyM3 http://www.numino.net
filContent=filContent & Chr(curCode)
cqb3Do http://www.numino.net
Else
Hhx4cg http://www.numino.net
nextCode=ascb(midb(xml.responseBody,i+1,1))
318YEA http://www.numino.net
filContent=filContent & Chr(CLng(curCode)*&H100+CInt(nextCode))
7Sje4E http://www.numino.net
:i=i+1
Zh1l1Q http://www.numino.net
End If
gj2xs0 http://www.numino.net
Next
b2BQuq http://www.numino.net
End If
ulCmlY http://www.numino.net
可是这里的lenb、midb、ascb什么的函数,在javascript中貌似没有相应的方法啊,网上查了很多资料,对此都是一个回答:无法解决。目前使用的方法,都是折中的处理办法,比如引用adodb.stream来进行字节转码:
dR9Z5L http://www.numino.net
function BytesToBstr(body,Cset){
HbhSpP http://www.numino.net
var objstream;
yKuZpn http://www.numino.net
objstream = new ActiveXObject("adodb.stream");
eOV7vx http://www.numino.net
objstream.Type = 1; //处理字节流
5IMjF7 http://www.numino.net
objstream.Mode =3; //读写模式
I6037I http://www.numino.net
objstream.open(); //打开对象
8CCdQR http://www.numino.net
objstream.write(body); //载入处理流
2vdCwH http://www.numino.net
objstream.Position = 0; //指针指向开始
prgfHA http://www.numino.net
objstream.Type = 2; //转换字符流处理
bynEQe http://www.numino.net
objstream.Charset = Cset; //设置字符集
dh6xf3 http://www.numino.net
var str=objstream.ReadText(); //读取字符
eVAzPe http://www.numino.net
objstream.close(); //关闭对向
Z7mDMS http://www.numino.net
return str; //返回字符串
Px3v3z http://www.numino.net
}
tCp128 http://www.numino.net
或者同一个网页中即使用vbscript函数,又使用javascript函数,将解码工作交给vbscript去做,然后javascript调用结果,没办法,谁叫javascript没有lenb,midb,ascb等牛叉的函数呢。 根据网上的一些例子改编的利用execScript混合vbscript片断的javascript解决办法。
0hjl5h http://www.numino.net
function getHTTPPage(sUrl){
HnPr8R http://www.numino.net
var Http;
c101iM http://www.numino.net
var Http=new ActiveXObject("MSXML2.XMLHTTP")
2cEJBA http://www.numino.net
Http.open("GET",sUrl,false);
EZDcBc http://www.numino.net
Http.send();
nqJCF8 http://www.numino.net
if(Http.readyState!= 4){
hZ05I7 http://www.numino.net
return false;
Nk8b1S http://www.numino.net
}
2QGLmx http://www.numino.net
return gb2utf8(Http.responseBody);
9ur0re http://www.numino.net
}
xVdKMY http://www.numino.net
function gb2utf8(data){
z78y38 http://www.numino.net
gb2utf8_data = data;
5wbxqn http://www.numino.net
execScript("gb2utf8_data = MidB(gb2utf8_data, 1)", "VBScript");
6Y6nq5 http://www.numino.net
var t=escape(gb2utf8_data).replace(/%u/g,"");
7LEBDq http://www.numino.net
t=t.replace(/(.{2})(.{2})/g,"%$2%$1");&nsp;
r7i8oq http://www.numino.net
t=t.replace(/%([A-Z].)%(.{2})/g,function(str){execScript("codeStr=chr("+eval(str.replace(/%(.{2})%(.{2})/g,"0x$1$2"))+")","VBScript");return codeStr});
73d68b http://www.numino.net
return unescape(t);
oFcFc4 http://www.numino.net
}
iR3UKR http://www.numino.net
看到这,忽然想想有没有javascript不利用外部工具来模拟vbscript的lenb,midb,ascb的实现呢,网上搜下关键字好象有人在提,先记录到这,如果能成,在把其间的两句execScript去掉,真正实现javascript解决这个问题。
更多相关内容...>>JavaScript中对XMLRequest的responseBody进行解码

Bug报告 |  免责声明 |  联系我们 |  加入收藏

Copyright © 2006 NuminoStudio(www.numino.net) All Rights Reserved