function Ajax(obj) { if(obj.method.toUpperCase() == "GET") { this.get(obj.url,obj.postData,obj.dataType,{success:obj.success,fail:obj.fail},obj.async); } else if (obj.method.toUpperCase() == "POST"){ this.post(obj.url,obj.postData,obj.dataType,{success:obj.success,fail:obj.fail},obj.async); } else { this.get(obj.url,obj.postData,obj.dataType,{success:obj.success,fail:obj.fail},obj.async); } } Ajax.prototype = { xhr: (function(){ var xhr = null; try { xhr = new XMLHttpRequest(); } catch (e) { try { xhr = new XMLHttpRequest("Msxml2.XMLHTTP"); } catch (e) { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } } return xhr; })(), post: function(url,data,dataType,callback,async) { var _xhr = this.xhr; _xhr.open("POST", url, async); _xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); _xhr.send(data); this.handle(_xhr,dataType,callback); }, get: function(url,data,dataType,callback,async) { var _xhr = this.xhr; _xhr .open("GET",url,async); _xhr.send(null); this.handle(_xhr,dataType,callback); }, handle: function(_xhr,dataType,callback) { var _dataType = dataType.toUpperCase(); _xhr.onreadystatechange = function() { if (_xhr.readyState == 4) { if (_xhr.status == 200) { var _data = (_dataType =="XML" ? _xhr.responseXML : _dataType == "JSON" ? (new Function("","return "+_xhr.responseText))() : _xhr.responseText); callback.success(_data); } else { var _data = (_dataType =="XML" ? _xhr.responseXML : _dataType == "JSON" ? (new Function("","return "+_xhr.responseText))() : _xhr.responseText); callback.fail(_data); } } } } }
new Ajax({ url:"http://localhost:8080/hsdj/getUserPower.hebe", postData:'', dataType:"json", async:"true", success:function(data){ for(var i in data) { alert(data); } }, fail:function(data){ }, method:"POST" });