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

📄 ajax.js

📁 最强的爬虫工程
💻 JS
字号:
/* 封装的AJAX类
 *
 * songjian 2007.03.06 v1.0 songjian@staff.sina.com.cn
 *
 * @param act		   string   提交方式 POST/GET
 * @param requesturl   string   提交的PHP文件
 * @param param        string   提交地址后跟的参数
 * @param returnformat string   以何种方式返回 TEXT/XML
 * @param event        function 回调的用户函数
 *
 * var request.readyState HTTP就绪状态
 *	   0:请求没有发出(在调用open()之前)
 *	   1:请求已经建立但还没有发出(调用send()之前)
 *	   2:请求已经发出正在处理之中(这里通常可以从响应得到内容头部)
 *	   3:请求已经处理,响应中通常有部分数据可用, 但是服务器还没有完成响应
 *	   4:响应已完成,可以访问服务器响应并使用它
 *
 * 使用方法:
 *	   html:
 *			<script type="text/javascript" src="ajax.js"></script> //引入js文件
 *			<div id='divid'></div>
 *			<script>
 *				var ajax = new MyAjax("POST", "http://localhost/mycode/js/test.php", "uid=20&title="+你好", "text", myfunction);
 *				function myfunction(response){
 *					document.getElementById('divid').innerHTML = response;
 *				}
 *			</script>
 *
/*---------------------------------------------------------*/

function Ajax(async){
	this.async = async ? true : false;

	this.init = function(){
		try{
			this.handler = new XMLHttpRequest();
			return true;
		}catch(e){
			try{
				this.handler = new ActiveXObject('Microsoft.XMLHTTP');
				return true;
			}catch(e){
				try{
					this.handler = new ActiveXObject('Msxml2.XMLHTTP');
					return true;
				}catch(e){
					return false;
				}
			}
		}
	}

	this.not_ready = function(){
		return (this.handler.readyState && (this.handler.readyState < 4));
	}
	
	this.onreadystatechange = function(event){
		if (!this.handler)
			this.init();
		
		if (typeof event == 'function') {
			this.handler.onreadystatechange = event;
		}else{
			alert('XML Sender OnReadyState event Must function');
		}
	}
	
	this.post = function(desturl, datastream){
		if (!this.handler) {
			this.init();
		}
		if (!this.not_ready()) {
			this.handler.open('POST', desturl, this.async);
			if (typeof(this.handler.setRequestHeader) != 'undefined') {
				this.handler.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			}
			this.handler.send(datastream);
			if (!this.async && this.handler.readyState == 4 && this.handler.status == 200) {
				return true;
			}
		}
		return false;
	}
	
	this.get = function(desturl, datastream) {
		if (!this.handler) {
			this.init();
		}
		if (!this.not_ready()) {
			this.handler.open('GET', desturl + "?" + datastream, this.async);
			this.handler.send(null);
			if (!this.async && this.handler.readyState == 4 && this.handler.status == 200) {
				return true;
			}
		}
		return false;
	}
}

function MyAJAX(act, requesturl, param, returnformat, event){
	this.act = act;
	this.requesturl = requesturl;
	this.param = param;
	this.returnformat = returnformat
	this.callback = event;
	this.xml = null;
	
	this.user = function(){
		this.xml = new Ajax(true);
		if (this.act.toLowerCase() == 'post'){
			this.xml.post(this.requesturl, this.param);
			this.xml.onreadystatechange(this.callBackResponse);
		} else if(this.act.toLowerCase() == 'get'){
			this.xml.get(this.requesturl, this.param);
			this.xml.onreadystatechange(this.callBackResponse);
		}
	}
	
	this.urlencode = function(text){
		return escape(text).replace(/\+/g, "%2B");
	}
	
	var me = this;
	
	this.callBackResponse = function(){		
		if (me.returnformat.toLowerCase() == 'xml'){
			if (me.xml.handler.readyState == 4 && me.xml.handler.status == 200 && me.xml.handler.responseXML){
				me.callback(me.xml.handler.responseXML);
				//alert(me.xml.handler.responseXML.documentElement);
			}
		} else {
			if (me.xml.handler.readyState == 4 && me.xml.handler.status == 200 && me.xml.handler.responseText){
				me.callback(me.xml.handler.responseText);
			}
		}
	}
	
	this.user();
}

⌨️ 快捷键说明

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