function createXHR(){ //检测原生XHR对象是否存在,如果存在刚返回它的新实例; //如果不存在,则检测ActiveX对象; //如果两个都不存在,就抛出一个错误。 if(typeof xmlHttPRequest != "undefined"){ return new xmlhttpRequest(); }else if(typeof ActiveXObject != "undefined"){ //适合IE7之前的版本 if(typeof arguments.callee.activeXString != "string"){ var versions = ["MSXML2.XMLHttp.6.0","MSXML2.XMLHttp.3.0","MSXML.XMLHttp"]; for(var i=0,len=versions.length; i<len; i++){ try{ var xhr = new ActiveXObject(versions[i]); arguments.callee.activeXString = versions[i]; return xhr; }catch (ex){ //跳过 } } } return new ActiveXObject(arguments.callee.activeXString); }else{ throw new Error("No XHR object available."); }; }
//创建XHR对象 var xhr = createXHR(); xhr.onreadystatechange = function(){ if(xhr.readyState == 4){ if((xhr.status >=200 && xhr.status < 300 ) || xhr.status == 304 ){ alert(xhr.responseText); }else{ alert("Request was unsuccessful : " + xhr.status); } } }
//读取example文本 xhr.open("get","example.txt",true); xhr.send(null);
新闻热点
疑难解答