⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 acc.js

📁 项目开发过程中用到的ajax源代码
💻 JS
字号:
function __System(){}

__System.IsString = function(value){
	return ((typeof(value) == "string")||((typeof value == "object") && (value instanceof String)));
}
__System.IsDate = function(value){
	return ((typeof value == "object") && (value instanceof String));
}
__System.IsRegExp = function(value){
	return ((typeof value == "object") && (value instanceof RegExp));
}
__System.IsArray = function(value){
	return ((typeof value == "object") && (value instanceof Array));
}
__System.IsNumber = function(value){
	return isNaN(value);
}
__System.IsBoolean = function(value){
	return ((typeof(value) == "boolean")||((typeof value == "object") && (value instanceof Boolean)));
}
function __Platform(){}
__System.Platform = __Platform;
__Platform.GetBrowser = function()
{
	if (typeof(navigator) == "undefined")
	{
		return "not-browser";
	}
	var ua = navigator.userAgent.toLowerCase();
	//if ((/gecko/i.test(ua)) && (parseFloat(ua.substr((ua.indexOf("firefox/") + 8),3)) >= 1.0))
	if (/gecko/i.test(ua))
	{
		return "mozilla";
	}
	else if (/opera/i.test(ua))
	{
		return "opera";
	}
	else if (/msie/i.test(ua))
	{
		if (/msie 7/i.test(ua))
		{
			return "ie7";
		}
		if (/msie 6/i.test(ua))
		{
			return "ie6";
		}
		else if (/msie 5\.5/i.test(ua))
		{
			return "ie5.5";
		}
		else if (/msie 5\.[^5]/i.test(ua))
		{
			return "ie5";
		}
		else
		{
			return "ie";
		}
	}
	return "other";
}
__Platform.Browser = __Platform.GetBrowser();
__Platform.BrowserIsIE = (__Platform.Browser.substr(0,2) == "ie");

//HTTP报头类
function EnumHttpHeader(){}
//请求报头
EnumHttpHeader.AcceptCharset = "Accept-Charset";
EnumHttpHeader.AcceptEncoding = "Accept-Encoding";
EnumHttpHeader.AcceptLanguage = "Accept-Language";
EnumHttpHeader.From = "From";
EnumHttpHeader.Host = "Host";
EnumHttpHeader.IfModifiedSince = "If-Modified-Since";
EnumHttpHeader.IfMatch = "If-Match";
EnumHttpHeader.IfNoneMatch = "If-None-Match";
EnumHttpHeader.IfRange = "If-Range";
EnumHttpHeader.IfUnmodifiedSince = "If-Unmodified-Since";
EnumHttpHeader.MaxForwards = "Max-Forwards";
EnumHttpHeader.ProxyAuthorization = "Proxy-Authorization";
EnumHttpHeader.Range = "Range";
EnumHttpHeader.Referer = "Referer";
EnumHttpHeader.UserAgent = "User-Agent";
//响应报头
EnumHttpHeader.Age = "Age";
EnumHttpHeader.Location = "Location";
EnumHttpHeader.ProxyAuthenticate = "Proxy-Authenticate";
EnumHttpHeader.Public = "Public";
EnumHttpHeader.RetryAfter = "Retry-After";
EnumHttpHeader.Server = "Server";
EnumHttpHeader.Warning = "Warning";
EnumHttpHeader.WWWAuthenticate = "WWW-Authenticate";
//通用报头
EnumHttpHeader.CatchControl = "Catch-Control";
EnumHttpHeader.Connection = "Connection";
EnumHttpHeader.Date = "Date";
EnumHttpHeader.Pragma = "Pragma";
EnumHttpHeader.TransferEncoding = "Transfer-Encoding";
EnumHttpHeader.Upgrade = "Upgrade";
EnumHttpHeader.Via = "Via";
//实体报头
EnumHttpHeader.Allow = "Allow";
EnumHttpHeader.ContentBase = "Content-Base";
EnumHttpHeader.ContentEncoding = "Content-Encoding";
EnumHttpHeader.ContentLanguage = "Content-Language";
EnumHttpHeader.ContentLength = "Content-Length";
EnumHttpHeader.ContentLocation = "Content-Location";
EnumHttpHeader.ContentMD5 = "Content-MD5";
EnumHttpHeader.ContentRange = "Content-Range";
EnumHttpHeader.ContentType = "Content-Type";
EnumHttpHeader.ETag = "ETag";
EnumHttpHeader.Expires = "Expires";
EnumHttpHeader.LastModified = "Last-Modified";


function HttpStatus(xmlHttpObj)
{
	if (arguments.length == 0){
		this.StatusCode = 0;
		this.StatusText = "";
	}
	else{
		this.StatusCode = xmlHttpObj.status;
		this.StatusText = xmlHttpObj.statusText;
	}
	
}




/*私有方法必须被定义在调用代码之前,否则会出错*****************/
AsyncCommunicator.prototype.$Pri_GetXmlHttpObj = function()
{
    var xmlHttp=false;
	var msXML = ['MSxml2.XMLHTTP.5.0','MSxml2.XMLHTTP.4.0','MSxml2.XMLHTTP.3.0','MSxml2.XMLHTTP','Microsoft.XMLHTTP'];
          for(var i = 0; i < msXML.length; i ++){
            try{
              xmlHttp = new ActiveXObject(msXML[i]);
              break;
            }
            catch(ex){
              xmlHttp = false;
            }
          }
	if (!xmlHttp && typeof(XMLHttpRequest) != 'undefined') {
	  xmlHttp = new XMLHttpRequest();
	}
	
	// mozilla某些版本没有readyState属性
	if(xmlHttp){
		if (xmlHttp.readyState == null){
          		xmlHttp.readyState = 0;
          		xmlHttp.addEventListener("load", function (){},false);
	    }
	}
	return xmlHttp;          
};

AsyncCommunicator.prototype.$Pri_SendWith4Args = function(sendingTarget,sendingMethod,contentType,data)
{
	if ((!__System.IsString(sendingTarget)) || (!__System.IsString(sendingMethod)) || (!__System.IsString(contentType)) || (!__System.IsString(data))){
		throw new Error("参数类型不正确!");
		return;
	}
	switch (sendingMethod.toLowerCase()){
		case "get":
			if (data != "" && contentType == "application/x-www-form-urlencoded"){
				sendingTarget = sendingTarget.concat("?",data);
				data = "";
			}
			break;
		case "post":
			break;
		default:
			throw new Error("不支持以" + sendingMethod + "方式提交数据。");
			break;	
	}
	//window.alert("open方法之前:" + this.XmlHttpObj.readyState);
	this.XmlHttpObj.open(sendingMethod,sendingTarget,true);
	//window.alert("open方法之后send方法之前:" + this.XmlHttpObj.readyState);
	if (contentType != ""){
		this.XmlHttpObj.setRequestHeader("Content-Type",contentType);			
	}
	if (this.HttpHeaders.length != 0){
		var len = this.HttpHeaders.length;
		var header;
		for (var i = 0 ; i < len ; i ++ ){
			header = this.HttpHeaders[i];
			this.XmlHttpObj.setRequestHeader(header[0],header[1]);
		}
	}
	if ((this.AbortableWhenTimeOut) && (this.TimeOut > 0)){
		window.setTimeout(this.$Pri_AbortWhenTimeOut(this),this.TimeOut,"javascript");
	}
	this.OnProcess(this);
	this.XmlHttpObj.send(data);
	//window.alert("send方法之后:" + this.XmlHttpObj.readyState);

};

AsyncCommunicator.prototype.$Pri_OnReceive = function()
{
	this.HttpStatus = new HttpStatus(this.XmlHttpObj);
	try {
		this.ReceivedBody = this.XmlHttpObj.responseBody;
	}
	catch(ex){
		this.ReceivedBody = "";
	}
	try {
		this.ReceivedText = this.XmlHttpObj.responseText;
	}
	catch(ex){
		this.ReceivedText = "";
	}
	try {
		this.ReceivedXML = this.XmlHttpObj.responseXML;
	}
	catch(ex){
		this.ReceivedXML = null;
	}
};			

AsyncCommunicator.prototype.$Pri_EventHandler  = function(source)
{
	return function()
	{
		switch(source.XmlHttpObj.readyState){
			case 3:
				source.OnTransferBack(source);
				break;
			case 4:
				if (!source.Pri_Aborted){
					source.Pri_HasReceived = true;
					source.$Pri_OnReceive();																
					source.OnReceive(source);
					try{
						switch(parseInt(((source.XmlHttpObj.status).toString()).charAt(0))){
							case 1:
								source.OnReceive1xx(source);
								break;
							//case 0:
							case 2:
								source.OnReceive2xx(source);
								break;
							case 3:
								source.OnReceive3xx(source);
								break;
							case 4:
								source.OnError(source);
								source.OnReceive4xx(source);
								break;
							case 5:
								source.OnError(source);
								source.OnReceive5xx(source);
								break;
							default:
								break;
						}
					}
					catch(ex){
					}
				}
				break;
			default:
				break;
		}
	}
	
};
AsyncCommunicator.prototype.Dispose = function()
{
	this.OnDispose(this);
	for(key in this){
		this[key] = null;
	}
};	

AsyncCommunicator.prototype.$Pri_AbortWhenTimeOut = function(source)
{
	//window.alert(this.toString());
	return function()
	{
		if(!source.Pri_HasReceived){
			source.Pri_Aborted = true;
			source.OnTimeOut(source);
			source.Abort();
		}
	}
	
}		
//公有属性
AsyncCommunicator.prototype.CurrentState = -1;
AsyncCommunicator.prototype.ReceivedBody = "";
AsyncCommunicator.prototype.ReceivedText = "";
AsyncCommunicator.prototype.ReceivedXML = null;
AsyncCommunicator.prototype.SendingMethod = "GET";
AsyncCommunicator.prototype.SendingTarget = "";
AsyncCommunicator.prototype.DataSent = "";
AsyncCommunicator.prototype.DataContentType = "application/x-www-form-urlencoded";
AsyncCommunicator.prototype.TimeOut = 10000;
AsyncCommunicator.prototype.AbortableWhenTimeOut = false;
AsyncCommunicator.prototype.HttpHeaders = new Array();

AsyncCommunicator.prototype.BatchSendResultArray = new Array();
AsyncCommunicator.prototype.BatchSendErrorCount = 0;
//私有属性
AsyncCommunicator.prototype.Pri_Aborted = false;
AsyncCommunicator.prototype.Pri_HasReceived = false;
//事件
AsyncCommunicator.prototype.OnProcess = function(source){};
AsyncCommunicator.prototype.OnTransferBack = function(source){};
AsyncCommunicator.prototype.OnReceive = function(source){};
AsyncCommunicator.prototype.OnReceive1xx = function(source){};
AsyncCommunicator.prototype.OnReceive2xx = function(source){};
AsyncCommunicator.prototype.OnReceive3xx = function(source){};
AsyncCommunicator.prototype.OnReceive4xx = function(source){};
AsyncCommunicator.prototype.OnReceive5xx = function(source){};
AsyncCommunicator.prototype.OnError = function(source){};
AsyncCommunicator.prototype.OnDispose = function(source){};
AsyncCommunicator.prototype.OnAbort = function(source){};
AsyncCommunicator.prototype.OnTimeOut = function(source){};

//方法
AsyncCommunicator.prototype.SetHttpRequestHeader = function(headerName,headerValue)
{
	if ((__System.IsString(headerName)) && (__System.IsString(headerValue))){
		var array = new Array();
		array[0] = headerName;
		array[1] = headerValue;
		this.HttpHeaders[this.HttpHeaders.length] = array;
	}
	else{
		throw new Error("不正确的HTTP请求头!");	
	}
}
AsyncCommunicator.prototype.GetHttpResponseHeader = function(headerName)
{
	if (__System.IsString(headerName)){
		return this.XmlHttpObj.getResponseHeader(headerName);
	}
	else{
		throw new Error("不正确的HTTP响应头!");	
	}
}
AsyncCommunicator.prototype.GetAllResponseHeaders = function()
{
		return this.XmlHttpObj.getAllResponseHeaders();
}
AsyncCommunicator.prototype.Abort = function()
{
	this.OnAbort(this);
	this.XmlHttpObj.abort();
}
AsyncCommunicator.prototype.Send = function(sendingTarget,sendingMethod,contentType,data)
{
	this.BatchSendResultArray = new Array();
	this.BatchSendErrorCount = 0;
	var numArgs = arguments.length;
	switch (numArgs){
		case 1:
			if ((typeof arguments[0] == "object") && (arguments[0] instanceof ACCRequest)){
				this.Send(arguments[0].SendingTarget,arguments[0].SendingMethod,arguments[0].ContentType,arguments[0].Data);
			}
			else{
				this.$Pri_SendWith4Args(arguments[0],this.SendingMethod,"",this.DataSent);
			}							
			break;
		case 2:										
			this.$Pri_SendWith4Args(arguments[0],arguments[1],"",this.DataSent);
			break;
		case 3:										
			this.$Pri_SendWith4Args(arguments[0],arguments[1],arguments[2],this.DataSent);
			break;
		case 4:
			this.$Pri_SendWith4Args(arguments[0],arguments[1],arguments[2],arguments[3]);
			break;
		default:
			throw new Error("传递给Send方法的参数个数不正确,只接受1到4个参数。");
			break;
	}
}	


function AsyncCommunicator()
{
	this.HttpStatus = new HttpStatus();
	this.XmlHttpObj = this.$Pri_GetXmlHttpObj();
	this.XmlHttpObj.onreadystatechange = this.$Pri_EventHandler(this);
}

												 								    

⌨️ 快捷键说明

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