本文主要是介绍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);};elsethis.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博客
版权所有:非特殊说明都是本站原创文章,转载请注明出处
这篇关于javascript封装的ajax函数库的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!