代码改变世界

ajax

2016-10-09 20:43  雄风狂飙  阅读(108)  评论(0)    收藏  举报
/**
 * 
 */

var net = new Object();

net.AjaxRequest = function(url,onload,onerror,method,params){
	this.req = null;
	this.onload = onload;
	this.onerror = onerror ? onerror:this.defaultError;
	this.loadDate(url,method,params);	
}

net.AjaxRequest.prototype.loadDate = function(url,method,params){
	if(!method)
	{
		method= "POST";
	}
	if(window.XMLHttpRequest){  //非IE
		this.req = new XMLHttpRequest();
	}else if(window.ActiveXOject){  //IE
		this.req = new ActiveXOject("Microsoft.XMLHTTP");
	}
	
	if(this.req){
		try{
			var loader = this;
			this.req.onreadystatechange = function(){
				net.AjaxRequest.onReadyState.call(loader);
			}
			
			
			this.req.open(method,url,true);

			if(method=="POST"){
				this.req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
				this.req.setRequestHeader("x-requested-with","ajax");
			}
			this.req.send(params);
		}catch(err){
			this.onerror.call(this);
		}

	}
}


net.AjaxRequest.onReadyState = function(){
	var req = this.req;
	var ready = req.readyState;
	if( ready==4 ){
		if(req.status==200){
			this.onload.call(this);
		}else{
			this.onerror.call(this);
		}
	}
}

net.AjaxRequest.prototype.defaulterror = function(){
	alert("错误数据\n\n回调状态"+this.req.readyState+"\n状态:"+this.req.status);
}