一个原生js操作Ajax封装

本文深入解析了Ajax类的初始化方法,包括请求类型、数据类型、成功回调和错误处理等核心参数的使用方法。
/*------------------------------------------
 * @ ajax类
------------------------------------------*/
M.ajax.prototype = {
	init : function (config) {
		var that = this;
		that.type = config.type ? config.type.toUpperCase() : "GET";
		that.dataType = config.dataType || "html";
		that.success = config.success || false;
		that.error = config.error || false;
		that.url = config.url || "";
		that.data = config.data || "";
		that.contentType = config.contentType ||"application/x-www-form-urlencoded";
		that.cache = config.cache === false ? false : true;
		that.request = that.getRequest();
		if(M.typeOf( that.data ) == "object"){
			var tmp = '';
			for(var k in that.data){
				tmp+='&'+k+'='+that.data[k];
			}
			if(tmp)that.data = tmp.replace("&","");
			tmp = null;
		}
		if (that.type == "GET") {
			var lj = (that.url.indexOf("?") > -1) ? "&" : "?";
			that.url += that.data ? (lj + that.data) : "";
		}
		if (!that.cache) {
			var now = new Date();
			now = now.getTime();
			var lj = (that.url.indexOf("?") > -1) ? "&" : "?";
			that.url += lj + '_=' + now;
		}
		if (that.request) {
			var req = that.request;
			req.onreadystatechange = that.bindFunction(that.stateChange, that);
			req.open( that.type , that.url, true);
			if (that.type == "POST"){
				req.setRequestHeader("Content-type", that.contentType);
				req.send(that.data);
			}else{
				req.send(null);
			}
		}
	},
	getRequest : function () {
		if (window.ActiveXObject)
			return new ActiveXObject('Microsoft.XMLHTTP');
		else if (window.XMLHttpRequest)
			return new XMLHttpRequest();
		return false;
	},
	bindFunction : function (caller, object) {
		return function () {
			return caller.apply(object, [object]);
		};
	},
	stateChange : function (object) {
		that = this;
		if (that.request.readyState == 4) {
			if (that.request.status == 200) {
				if (that.dataType == "json") {
					try {
						if (typeof JSON === "object") {
							var json = JSON.parse(that.request.responseText);
						} else {
							var json = eval('(' + that.request.responseText + ')');
						}
					} catch (e) {
						if (that.error) {
							that.error();
							return false;
						}
					}
					if (typeof json === "object") {
						if (that.success) {
							that.success(json,that.request.status,that.request);
							return true;
						}
					} else {
						if (that.error) {
							that.error();
							return false;
						}
					}
				} else {
					if (that.success) {
						that.success(that.request.responseText,that.request.status,that.request);
						return true;
					}
				}
			} else {
				if (that.error) {
					that.error(that.request);
					return false;
				}
			}
		}
	}
}
M.ajax.prototype.init.prototype = M.ajax.prototype;


版权所有:www.meilele.com

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值