JavaScript中对XMLRequest的responseBody进行解码 |
前面一篇日记学习了XMLHTTP的基本用法,用responseText其获得网页的内容如有Unicode字符的时候,则会显示乱码, tETex7 http://www.numino.net yDLm43 http://www.numino.net 咋处理呢?这个时候还有另一个对象responseBody,看下它的运行结果: sY57BD http://www.numino.net CGNXhM http://www.numino.net 这个结果……更不像话,原因是因为responseBody存储的是字节,如果用vbsEdit来调试VBS版的XMLHTTP的话,则可以在DEBUG界面看到responseBody中存储的一个个字节形字数据,问题就出在这里了,在VBS中,可以用midb,ascb等手段将字节正确地解码,方式如下: RYHn1x http://www.numino.net Set xml=CreateObject("msxml2.xmlhttp") F8nxV9 http://www.numino.net xml.open "get",url,False 5W3CRM http://www.numino.net xml.send lh07fC http://www.numino.net If xml.readyState=4 And xml.status=200 Then S5cOs5 http://www.numino.net 'WScript.Echo lenb(xml.responseBody) u4i4L7 http://www.numino.net For i=1 To lenb(xml.responseBody) Mpqdm1 http://www.numino.net curCode=ascb(midb(xml.responseBody,i,1)) B7o23a http://www.numino.net If curCode<&h80 Then UAoTJY http://www.numino.net filContent=filContent & Chr(curCode) N9iBAs http://www.numino.net Else Sj69Id http://www.numino.net nextCode=ascb(midb(xml.responseBody,i+1,1)) 83114X http://www.numino.net filContent=filContent & Chr(CLng(curCode)*&H100+CInt(nextCode)) cb99lS http://www.numino.net :i=i+1 3uyepo http://www.numino.net End If 849Js5 http://www.numino.net Next Dqfahf http://www.numino.net End If aa41tF http://www.numino.net 可是这里的lenb、midb、ascb什么的函数,在javascript中貌似没有相应的方法啊,网上查了很多资料,对此都是一个回答:无法解决。目前使用的方法,都是折中的处理办法,比如引用adodb.stream来进行字节转码: VGQ02l http://www.numino.net function BytesToBstr(body,Cset){ hSFALu http://www.numino.net var objstream; Q9Aap5 http://www.numino.net objstream = new ActiveXObject("adodb.stream"); w4JNsy http://www.numino.net objstream.Type = 1; //处理字节流 SBF6RL http://www.numino.net objstream.Mode =3; //读写模式 11codm http://www.numino.net objstream.open(); //打开对象 1sEqE1 http://www.numino.net objstream.write(body); //载入处理流 SU9cHl http://www.numino.net objstream.Position = 0; //指针指向开始 VoCKNu http://www.numino.net objstream.Type = 2; //转换字符流处理 4YDLp9 http://www.numino.net objstream.Charset = Cset; //设置字符集 1H8b1o http://www.numino.net var str=objstream.ReadText(); //读取字符 dANj10 http://www.numino.net objstream.close(); //关闭对向 Yg4caJ http://www.numino.net return str; //返回字符串 5n6eu5 http://www.numino.net } u4KITe http://www.numino.net 或者同一个网页中即使用vbscript函数,又使用javascript函数,将解码工作交给vbscript去做,然后javascript调用结果,没办法,谁叫javascript没有lenb,midb,ascb等牛叉的函数呢。 根据网上的一些例子改编的利用execScript混合vbscript片断的javascript解决办法。 542Zhy http://www.numino.net function getHTTPPage(sUrl){ fop0Qp http://www.numino.net var Http; Y0apvs http://www.numino.net var Http=new ActiveXObject("MSXML2.XMLHTTP") SvGP2r http://www.numino.net Http.open("GET",sUrl,false); 2X85me http://www.numino.net Http.send(); oMXAMn http://www.numino.net if(Http.readyState!= 4){ FOD80d http://www.numino.net return false; pjx627 http://www.numino.net } WG9jY9 http://www.numino.net return gb2utf8(Http.responseBody); A8pCy7 http://www.numino.net } aUplN7 http://www.numino.net function gb2utf8(data){ jqstPW http://www.numino.net gb2utf8_data = data; I7Bib9 http://www.numino.net execScript("gb2utf8_data = MidB(gb2utf8_data, 1)", "VBScript"); q4WCDq http://www.numino.net var t=escape(gb2utf8_data).replace(/%u/g,""); 6QrMe7 http://www.numino.net t=t.replace(/(.{2})(.{2})/g,"%$2%$1");&nsp; qNhYuW 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}); 5VKS3A http://www.numino.net return unescape(t); 7Ci8pJ http://www.numino.net } 91shho http://www.numino.net 看到这,忽然想想有没有javascript不利用外部工具来模拟vbscript的lenb,midb,ascb的实现呢,网上搜下关键字好象有人在提,先记录到这,如果能成,在把其间的两句execScript去掉,真正实现javascript解决这个问题。
|
|