ajax.js

来自「LiteBlaster 是一款基于 .NET Framework 原创的 ASP」· JavaScript 代码 · 共 53 行

JS
53
字号
//sarissa.js needed

var ajax = new Object();
ajax.READY_STATE_COMPLETE = 4;
ajax.loader = function(url, method, onload, onerror, data, contentType){
	this.req = null;
	this.onload = onload;
	this.onerror = (onerror) ? onerror : this.defaultError;
	this.send(url, method, data, contentType);
}
ajax.loader.prototype = {
	send : function(url, method, data, contentType){
		if(!method){
			method = 'GET';
		}
		if(!contentType && method == 'POST'){
			contentType = 'application/x-www-form-urlencoded';
		}
		this.req = new XMLHttpRequest();
		if(!this.req){
			this.onerror.call(this);
		}
		try{
			var loader = this;
			this.req.onreadystatechange = function(){
				loader.onReadyState.call(loader);
			}
			this.req.open(method, url, true);
			if(contentType){
				this.req.setRequestHeader('Content-Type', contentType);
			}
			this.req.send(data);
		}catch(e){
			this.onerror.call(this);
		}
	},
	
	onReadyState : function(){
		var req = this.req;
		if(req.readyState == ajax.READY_STATE_COMPLETE){
			var httpStatus = req.status;
			if(httpStatus == 200 || httpStatus == 0){
				this.onload.call(this);
			}else{
				this.onerror.call(this);
			}
		}
	},
	
	defaultError : function(){
		alert('请求失败,请重试。');
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?