function createXHR() {
if(typeof XMLHttpRequest != "undefined") {
return new XMLHttpRequest();
} else if(typeof ActiveXObject != "undefined") {
if(typeof arguments.callee.activeXString != "string") {
var versions = ["Msxml3.XMLHTTP", "MSXML2.XMLHttp.6.0", "MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp", "Microsoft.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.")
}
}
var xhr = createXHR();
xhr.onreadystatechange = function() {
if(xhr.readyState == 4) {
if((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {
console.log(xhr.responseText)
}
}
};
xhr.open("post", "url", false);//ture同步,false 异步
xhr.withCredentials = true; //跨域
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(data);
原生xhr请求
最新推荐文章于 2024-06-08 06:57:39 发布