XML HttpRequest 실례

2553 단어 AjaxXMLhttpREquest
// JavaScript source code

//  httprequest  
var http=getHttpObject();

function getHttpObject()
{
	var xmlhttp=false;
	//Mozilla、Safari  IE   
	if(window.XMLHttpRequest)
	{
		xmlhttp=new XMLHttpRequest();
	/*            XML mime-type ,
	  Mozilla                。
	        ,           text/xml,            。
	*/
		if(xmlhttp.overrideMimeType)
		{
			xmlhttp.overrideMimeType('text/xml');
		}
	}
	//IE   
	else
	{
	//    IE
		try{
			xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e)
		{
			try
			{
		//    IE
				xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			}catch(E)
			{
				xmlhttp=false;
			}
		}
	}
	return xmlhttp;
}
//  request  
function sendHttpRequest()
{
	//     http        
	if(!http)
	{
		alert("The XMLHttpRequest Can't be created!");
		return false;
	}
	var url="/do/login";
	//              ,                            ;
	//  GET    
	url+="?username=davidchow&userpassword=james";
	http.open('GET',url,true);
	http.onreadystatechange=processHttpRequest;
	http.send(null);
	/*  POST    
	http.open('POST',url,true);
	http.onreadystatechange=processHttpRequest;
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
	http.send("username=davidchow&userpassword=james");
	*/
}
//       response  
function processHttpRequest()
{
	if (http.readyState == 4) { 
		//     ,      
            if (http.status == 200) {
            	//   !         !          
            	//             (msg  HTML       ID )
            	document.getElementById("msg").innerText=http.responseText;
				/*                   !
				document.getElementById("msg").innerHTML=http.responseText;
            	      xml  ,       XML                  !
            	http.responseXML;
            	*/
            	
            } else {
            	alert("this page is err");
            }
        }
        else
        {
			//    !           !
			alert("Please Wait!Loading....!")
        }
}

 
 
 

좋은 웹페이지 즐겨찾기