整理了一下javascript 原生ajax的兼容写法,超级简洁的代码!
function ajax(url,callback) {
var xmlHttp;
if (window.ActiveXObject){
var arr=["MSXML2.XMLHttp.6.0","MSXML2.XMLHttp.5.0", "MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0","MSXML2.XMLHttp","Microsoft.XMLHttp"];
for(var i=0;i<arr.length;i++) {
try {
xmlHttp = new ActiveXObject(arr[i]);
if(xmlHttp){break}
}
catch(error) { }
}
} else {
try {
xmlHttp=new XMLHttpRequest();
}
catch(otherError) { }
}
xmlHttp.open('GET',url,true);
xmlHttp.onreadystatechange = function() {
if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
var result = xmlHttp.responseText;
callback(result);
}
}
xmlHttp.send();
}