javascript封装的ajax函数库
下面是封装好的ajax的函数
function AjaxObj() { this.xmlHttp = null; this.Request = function(method, url, data, callback, sync) { if (window.ActiveXObject) { this.xmlHttp = new ActiveXObject("Microsoft.XMLHttp"); } else if (window.XMLHttpRequest) { this.xmlHttp = new XMLHttpRequest(); if (this.xmlHttp.overrideMimeType) { this.xmlHttp.overrideMimeType('text/xml'); } } if (this.xmlHttp) { var self = this; if (callback) this.xmlHttp.onreadystatechange = function(){callback(self.xmlHttp);}; else this.xmlHttp.onreadystatechange = function(){return;}; if (!method) method = "POST"; method = method.toUpperCase(); if (method == 'GET') { this.xmlHttp.open('GET', url + ((typeof data=="string")?('?' + data):""), typeof sync == "boolean" ? sync : true); this.xmlHttp.send(null); } else { this.xmlHttp.open('POST', url, typeof sync == "boolean" ? sync : true); this.xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); this.xmlHttp.send(data); } } }; this.abort = function() { if (this.xmlHttp) this.xmlHttp.abort(); }; this.swRequest = function(cfg){ if(!cfg.url) return; this.Request(cfg.method||"POST",cfg.url||"",cfg.data,function(req){ if(req.readyState==4){ if(req.status==200||req.status==0){ var obj = null; var text = req.responseText; eval("obj = "+ text); cfg.success.call(cfg.soap||this,obj); return; }else{ cfg.failure.call(cfg.soap||this,"错误!"); return; } } }); }; }
调用方法:
ajax.swRequest({ method:"POST", sync:false, url:'?a=manage&m=checkUser', data:"user="+user.value, success: function(msg) { if(msg==1){ flag.value = 'true'; } else { flag.value = ''; } }, failure: function(a) { alert(a); }, soap:this });
本文链接:javascript封装的ajax函数库
联系作者:javascript博客
版权所有:非特殊说明都是本站原创文章,转载请注明出处
相关推荐
TONIYH 2020-06-05
坚持着执着 2020-07-16
坚持着执着 2020-06-14
chongxiaocheng 2020-08-16
ppsurcao 2020-06-14
kentrl 2020-11-10
结束数据方法的参数,该如何定义?-- 集合为自定义实体类中的结合属性,有几个实体类,改变下标就行了。<input id="add" type="button" value="新增visitor&quo
ajaxyan 2020-11-09
zndy0 2020-11-03
学留痕 2020-09-20
Richardxx 2020-11-09
learningever 2020-09-19
ajaxhe 2020-08-16
lyqdanang 2020-08-16
curiousL 2020-08-03
TONIYH 2020-07-22
时光如瑾雨微凉 2020-07-19
83510998 2020-07-18
jiaguoquan00 2020-07-07