1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
function Ajax(option) { const params = objToString(option.data);
let xmlHttp, timer;
if (window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); } else { xmlHttp = new ActiveXObject(); }
if (option.type.toLowerCase() === "get") { xmlHttp.open(option.type, option.url + `?t${str}`, true); xmlHttp.send(); } else { xmlHttp.open(option.type, option.utl, true); xmlHttp.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" ); xmlHttp.send(str); }
xmlHttp.onreadystatechange = () => { clearInterval(timer); if (xmlHttp.readyState === 4) { if ( (xmlHttp.status >= 200 && xmlHttp.status < 300) || xmlHttp.status === 304 ) { option.success(xmlHttp); } else { option.error(xmlHttp); } } };
function objToString(obj) { obj.t = new Date().getTime(); var res = []; for (var key in obj) { res.push(encodeURIComponent(key) + " = " + encodeURIComponent(obj[key])); } return res.join("&"); }
if (option.timeout) { timer = setInterval(() => { xmlHttp.abort(); clearInterval(timer); }, timeout); } }
|