标签:
//兼容性XHR
function createXHR(){
if(typeof XMLHttpRequest !="undefined"){
return new XMLHttpRequest();
}else if(typeof ActiveXObject !="undefined"){
if(typeof arguments.callee.activeString!="string"){
var versions=["MSXML2.XMLHTTP6.0","MSXML2.XMLHTTP3.0","MSXML2.XMLHTTP"], i,len;
for(i=0;len=versions.lengthli<len;i++){
try{
new ActiveXObject(versions[i]);
arguments.callee.activeString=versions[i];
break;
}catch (ex){
//跳过
}
}
}
return new ActiveXObject(arguments.callee.activeString);
}else{
throw new Error("无可用XHR");
}
}
//兼容性写法 var xhr=new createXHR();
var xhr=new XMLHttpRequest();
xhr.onreadystatechange=function(){
if(xhr.readyState==4){
if((xhr.status>=200 && xhr.status<300) || xhr.status==304){
alert(xhr.responseText);
}else{
alert("请求失败!");
}
}
};
xhr.open("get","zqz.txt",true);
xhr.send(null);
结果:
标签:
原文地址:http://www.cnblogs.com/zqzjs/p/4912623.html