ajax返回乱码的解决方案

一,通过VBS转换函数
VBS转换函数:
Function convertstr(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
convertstr = strReturn
End Function


//ajax处理
var http_request = false;
function send_request(url,obj){//初始化、指定处理函数、发送请求的函数
var obj=obj;
http_request = false; //开始初始化XMLHttpRequest对象
if(window.XMLHttpRequest) { //Mozilla 浏览器
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {//设置MiME类别
http_request.overrideMimeType("text/html");//设定以gb2312编码识别数据
}
}else if (window.ActiveXObject) { // IE浏览器
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e){
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) { // 异常,创建对象实例失败
window.alert("不能创建XMLHttpRequest对象实例.");
return false;
}
http_request.onreadystatechange =function(){
if (http_request.readyState == 4) { // 判断对象状态
if (http_request.status == 200) { // 信息已经成功返回,开始处理信息
gb2utf8_data =http_request.responseBody;
execScript("gb2utf8_data = MidB(gb2utf8_data, 1)", "VBScript");
var t=escape(gb2utf8_data).replace(/%u/g,"").replace(/(.{2})(.{2})/g,"%$2%$1").replace(/%([A-Z].)%(.{2})/g,"@$1$2");
document.getElementById(obj).innerHTML=bytes2BSTR(http_request.responseBody);
//alert(bytes2BSTR(http_request.responseBody));
} else{ //页面不正常
document.getElementById(obj).innerHTML="发生错误!访问超时!"
}
}
}
http_request.open("POST",url, true);
http_request.send(null);
} // 处理返回信息的函数


二.通过JS转换函数gb2utf8:

function gb2utf8(data){
var glbEncode = [];
gb2utf8_data = data;
execScript("gb2utf8_data = MidB(gb2utf8_data, 1)", "VBScript");
var t=escape(gb2utf8_data).replace(/%u/g,"").replace(/(.{2})(.{2})/g,"%$2%$1").replace(/%([A-Z].)%(.{2})/g,"@$1$2");
t=t.split("@");
var i=0,j=t.length,k;
while(++i<j) {
k=t.substring(0,4);
if(!glbEncode[k]) {
gb2utf8_char = eval("0x"+k);
execScript("gb2utf8_char = Chr(gb2utf8_char)", "VBScript");
glbEncode[k]=escape(gb2utf8_char).substring(1,6);
}
t=glbEncode[k]+t.substring(4);
}
gb2utf8_data = gb2utf8_char = null;
return unescape(t.join("%"));
}

用法和上面的一样,如:
document.getElementById(obj).innerHTML=gb2utf8(http_request.responseBody);

你可能感兴趣的:(Ajax,浏览器,IE,Microsoft,VBScript)