日期:2014-05-16  浏览次数:20697 次

Ajax的基本函数的实现
/*get a XMLHttpRequest from different browser*/
function getXMLHttpRequest(){
	if(window.XMLHttpRequest){
		/* Not IE*/
		return new XMLHttpRequest();
	}else if(window.ActiveXObject){
		/*IE*/
		return new ActiveXObject("Microsoft.XMLHTTP")
	}else{
		/*don't support Ajax*/
		alert("soory your browser doesn't support AJAX");
	}
}

/*ajax_simple_Request*/
function ajax_get_msg(){
	var xo = getXMLHttpRequest();
	if(xo.readyState == 4 || xo.readyState == 0){
		xo.open("GET","http://localhost:81/day3/testjsp.jsp",true);
		xo.onreadystatechange = function(){ //set the callback
			if(xo.readyState == 4){
				alert(xo.responseText);
			}
		}; 
		xo.send(null);
	}
}

/*get_Request*/
function ajax_get(id){
	var xo = getXMLHttpRequest();
	if(xo.readyState == 4 || xo.readyState == 0){
		xo.open("GET","http://localhost:81/day3/testjsp.jsp?id=" + id,true);
		xo.onreadystatechange = function(){ //set the callback
			if(xo.readyState == 4){
				alert(xo.responseText);
			}
		}; 
		xo.send(null);
	}
}

/*post_Request*/
function ajax_post(){
	var xo = getXMLHttpRequest();
	if(xo.readyState == 4 || xo.readyState == 0){
		xo.open("POST","http://localhost:81/day3/testjsp.jsp",true);
		xo.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
		xo.onreadystatechange = function(){ //set the callback
			if(xo.readyState == 4){
				alert(xo.responseText);
			}
		}; 
		xo.send("id=zhougege");
	}
}

?