简洁的Ajax代码
function initXMLhttp() {
var e;
return e = window.XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject("Microsoft.XMLHTTP")
}
function mysend(o, t, mypara) {
o.send(t);
}
function minAjax(e) {
caches.delete("threejs-editor");
var mypara = e.data;
var progress = e.progress;
var cache = false;
if (!e.url) return void (1 == e.debugLog && console.log("No Url!"));
if (!e.type) return void (1 == e.debugLog && console.log(
"No Default type (GET/POST) given!"));
e.method != null || (e.method = !0),
e.debugLog != null || (e.debugLog = !1);
var o = initXMLhttp();
if (e.rtype) {
o.responseType = e.rtype;
}
var t = [],
n = e.data;
o.onprogress = function (e) {
e = e || event;
var total = e.total;
if (total == 0) {
try {
total = e.srcElement.getResponseHeader('content-length');
} catch (e) {
console.error(e);
}
}
// if (e.lengthComputable) {
//result.innerHTML = "Received " + e.loaded + " of " + e.total + " bytes";
if (progress) {
progress(e.loaded, total, mypara);
}
// }
};
o.onreadystatechange = function () {
if (4 == o.readyState && e.error && o.status != 200) {
e.error(o.responseText);
return;
}
if (4 == o.readyState && 200 == o.status) {
//(e.success && e.success(o.responseText, o.readyState), 1 == e.debugLog && console.log("SuccessResponse"), 1 == e.debugLog && console.log("Response Data:" + o.responseText));
if (cache) {
var p = e.url + t;
if (!window.mycaches) {
window.mycaches = {};
}
window.mycaches[p] = o.responseText;
checkresult(p, e.mypara);
} else {
if (e.rtype) {
e.success && e.success(o.response, o.readyState);
} else {
(e.success && e.success(o.responseText, o.readyState), 1 == e.debugLog &&
console.log("SuccessResponse"), 1 == e.debugLog && console.log(
"Response Data:" + o.responseText));
}
}
} else {
1 == e.debugLog && console.log("FailureResponse --> State:" + o.readyState +
"Status:" + o.status);
}
// 4 == o.readyState && 200 == o.status ? (e.success && e.success(o.responseText, o.readyState), 1 == e.debugLog && console.log("SuccessResponse"), 1 == e.debugLog && console.log("Response Data:" + o.responseText)) : 1 == e.debugLog && console.log("FailureResponse --> State:" + o.readyState + "Status:" + o.status);
};
var contenttype = e.contenttype;
if (contenttype == null) {
contenttype = "application/x-www-form-urlencoded";
}
if ("string" == typeof n)
for (var s = String.prototype.split.call(n, "&"), r = 0, a = s.length; a > r; r++) {
var c = s[r].split("=");
t.push(encodeURIComponent(c[0]) + "=" + encodeURIComponent(c[1]));
t = t.join("&");
} else if ("object" == typeof n && !(n instanceof String || FormData && n instanceof FormData)) {
for (var p in n) {
var c = n[p];
if ("[object Array]" == Object.prototype.toString.call(c))
for (var r = 0,
a = c.length; a > r; r++) t.push(encodeURIComponent(p) + "[]=" +
encodeURIComponent(c[r]));
else t.push(encodeURIComponent(p) + "=" + (c == null ? "" : encodeURIComponent(
c)))
}
t = t.join("&");
} else {
t = n;
}
"GET" == e.type && (o.open("GET", e.url + "?" + t, e.method), mysend(o), 1 == e
.debugLog &&
console.log("GET fired at:" + e.url + "?" + t)),
"POST" == e.type && (o.open("POST", e.url, e.method), o.setRequestHeader(
"Content-type",
contenttype), mysend(o, t, mypara), 1 == e.debugLog &&
console.log(
"POST fired at:" + e.url + " || Data:" + t))
}
function ajax(method, url, data, success, error) {
minAjax({
url: url,
type: method.toUpperCase(),
data: data,
success: function (data) {
success && success(data);
},
error: function (data) {
error && error(data);
}
});
}
function post(url, data, success, error, contenttype, rtype) {
minAjax({
rtype: rtype,
contenttype: contenttype,
url: url,
type: "POST",
data: data,
success: function (data) {
success && success(data);
},
error: function (data) {
if (error) {
error(data);
} else {
alert(data);
}
}
});
}