function ajax(myjason) {
var xhr = null;
window.XMLHttpRequest?(xhr = new XMLHttpRequest()):xhr = new ActiveXObject("Microsoft.XMLHTTP");
var method = myjason.method || "get";
var url = myjason.url;
var asyn = myjason.asyn;
var data = myjason.data;
var fn = myjason.fn;
if (method == "get" && data) {
xhr.open(method, url + "?" + data + "&" + Math.random(), asyn);
}
if (method == "post" && data) {
xhr.open(method, url, asyn);
xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded");
xhr.send(data);
}
else {
xhr.send(data);
}
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status >= 200 && xhr.status < 300) {
fn(xhr.responseText);
}
else {
console.error("程序出错")
}
}
}
}
原生js实现ajax
最新推荐文章于 2024-04-30 16:21:35 发布