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
| function AJAX(obj) { var ajaxObj = null; if(window.XMLHttpRequest) { ajaxObj = new XMLHttpRequest(); } else { ajaxObj = new ActiveObject("Microsoft.XMLHTTP"); } ajaxObj.onreadystatechange = function() { if(ajaxObj.readyState == 4) { if(ajaxObj.status >= 200 && ajaxObj.status < 300 || ajaxObj.status == 304) { if(obj.success) { obj.success(JSON.parse(ajaxObj.responseText)); } else { alert("您忘记了 success 函数"); } } else { if(obj.error) { obj.error(ajaxObj.status); } else { alert("您忘记了 error 函数"); } } } } var type = obj.type || "get"; type = type.toLowerCase(); var params = ""; if(obj.data) { for(var key in obj.data) { params += (key + "=" + obj.data[key] + "&"); } params = params.slice(0, params.length - 1); } if(type == "get") { ajaxObj.open(type, obj.url + "?" + params, true); ajaxObj.send(null); } else { ajaxObj.open(type, obj.url, true); ajaxObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); ajaxObj.send(params); } }
|