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
|
function ajax(obj) { obj.type = obj.type || "get"; obj.async = obj.async || true; obj.data = obj.data || null;
if (window.XMLHttpRequest) var ajax = new XMLHttpRequest(); else var ajax = new ActiveXObject("Microsoft.XMLHTTP");
if (obj.data) { var data = "" Object.keys(obj.data).forEach(function (key) { data += "&" + key + "=" + obj.data[key] }); data = data.substring(1) }
if (obj.type == "post") { ajax.open(obj.type, obj.url, obj.async); ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); ajax.send(data); } else { var url = "" if (obj.data) url = obj.url + "?" + data else url = obj.url ajax.open(obj.type, url, obj.async); ajax.send(); }
ajax.onreadystatechange = function () { if (ajax.readyState == 4) { if (ajax.status >= 200 && ajax.status < 300 || ajax.status == 304) { if (obj.success) { obj.success(ajax.responseText); } } else { if (obj.error) { obj.error(ajax.status); } } } } }
|