function request(ops) {
let { url, success, type = "get", data = {}, error, timeout = 1000, dataType = "text", filedName = "" } = ops;
let str = '';
for (var i in data) {
str += `${i}=${data[i]}&`;
}
if (type !== "post") {
url = url + "?" + str + "_qft_=" + Date.now();
}
let xhr;
if (type !== "jsonp") {
xhr = new XMLHttpRequest();
xhr.open(type, url);
xhr.onload = function() {
if (xhr.status == 200) {
const res = dataType === "json" ? JSON.parse(xhr.responseText) : xhr.responseText;
success && success(res);
error = null;
success = null;
} else {
error && error(xhr.status);
success = null;
error = null;
}
}
if (type == "get") {
xhr.send();
} else {
xhr.setRequestHeader("Content-Type", "application/x-www-urlencoded")
xhr.send(str.slice(0, str.length - 1));
}
} else {
let script = document.createElement("script");
script.id = "abc";
script.src = url;
document.body.appendChild(script);
window[data[filedName]] = function(res) {
res = dataType === "json" ? JSON.parse(res) : res;
success && success(res);
script && script.remove();
error = null;
success = null;
}
}
setTimeout(() => {
error && error("timeout");
const s = document.getElementById("abc");
s && s.remove();
success = null;
error = null;
}, timeout)
}