function creatXHR(){
if(typeof XMLHttpRequest !="undefined"){
return new XMLHttpRequest();
}else if(typeof ActiveXObjct !="undefined"){
if(typeof arguments.callee.activeXString !="String"){
var versions =["MSXML2.XMLHttp.6.0","MSXML2.XMLHttp.3.0","MSXML.XMLHttp"],i,len;
for(i=0,len=versions.length;i<len;i++){
try(){
new ActiveXObject(versions[i]);
arguments.calee.activeXString = version[i];
break;
}catch(ex){
// 跳过
}
}
}
}else{
throw new Error("No XHR object avaliable");
}
}
// 正常情况先得同步请求:
var xhr = creatXHR();
xhr.open("get","exapmple.txt",false);
xhr.send(null);
if((xhr.status>=200 && xhr.status<300) || xhr.status===304){
alert(xhr.responseText);
}else{
alert("Request was unsuccessful :"+xhr.status);
}
// 异步请求:
var xhr = creatXHR();
xhr.onreadystatechange = function(){
if(xhr.readyState==4){
if((xhr.status>=200 && xhr.status<300) || xhr.status==304){
alert(xhr.responseText);
}else{
alert("Response was unsunnceeeful :"+xhr.status);
}
}
}
xhr.open("get","expamle.txt",true); //参数1:请求方式; 参数2:请求地址;参数三: 是否异步请求
xhr.send(null);
if(typeof XMLHttpRequest !="undefined"){
return new XMLHttpRequest();
}else if(typeof ActiveXObjct !="undefined"){
if(typeof arguments.callee.activeXString !="String"){
var versions =["MSXML2.XMLHttp.6.0","MSXML2.XMLHttp.3.0","MSXML.XMLHttp"],i,len;
for(i=0,len=versions.length;i<len;i++){
try(){
new ActiveXObject(versions[i]);
arguments.calee.activeXString = version[i];
break;
}catch(ex){
// 跳过
}
}
}
}else{
throw new Error("No XHR object avaliable");
}
}
// 正常情况先得同步请求:
var xhr = creatXHR();
xhr.open("get","exapmple.txt",false);
xhr.send(null);
if((xhr.status>=200 && xhr.status<300) || xhr.status===304){
alert(xhr.responseText);
}else{
alert("Request was unsuccessful :"+xhr.status);
}
// 异步请求:
var xhr = creatXHR();
xhr.onreadystatechange = function(){
if(xhr.readyState==4){
if((xhr.status>=200 && xhr.status<300) || xhr.status==304){
alert(xhr.responseText);
}else{
alert("Response was unsunnceeeful :"+xhr.status);
}
}
}
xhr.open("get","expamle.txt",true); //参数1:请求方式; 参数2:请求地址;参数三: 是否异步请求
xhr.send(null);