ajax

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>ajax</title>
	<script>
 /**
  *    onreadystatechange  每次狀態改變所觸發事件的事件處理程序。
  *  responseText     從服務器進程返回數據的字符串形式。
  *  responseXML    從服務器進程返回的DOM兼容的文檔數據對象。
  *  status           從服務器返回的數字代碼,比如常見的404(未找到)和200(已就緒)
  *  status Text       伴隨狀態碼的字符串信息
  *  readyState       對象狀態值
 *   0 (未初始化) 對象已建立,但是尚未初始化(尚未調用open方法)
 *   1 (初始化) 對象已建立,尚未調用send方法
 *   2 (發送數據) send方法已調用,但是當前的狀態及http頭未知
 *   3 (數據傳送中) 已接收部分數據,因爲響應及http頭不全,這時通過responseBody和responseText獲取部分數據會出現錯誤,
 *   4 (完成) 數據接收完畢,此時可以通過通過responseXml和responseText獲取完整的迴應數據
  */

function createXHR(){
	if(typeof XMLHttpRequest != "undefined"){
		return new XMLHttpRequest();
	}else if(typeof ActiveXObject != "undefined"){
             if(typeof arguments.callee.activeXString != "string"){
             		var versions=["MSXML2.XMLHttp.6.0","MSXML2.XMLHttp.3.0","MSXML2.XMLHttp"],
             			i,
             			len;
             			for (i = 0,len=versions.length; i < len; i++) {
             				try{
             					new ActiveXObject(versions[i]);
             					arguments.callee.activeXString=versions[i];
             					break;
             				}catch(ex){
             					console.log("error:  "+ex);
             				}
             			};

             }
             return new ActiveXObject(arguments.callee.activeXString);
	}else{
		throw new Error("No XHR object available!");
	}
}
	var xhr=createXHR();
	xhr.onreadystatechange=function(){
		if(xhr.readyState==4){
			if((xhr.status>=200 && xhr.status<300) || xhr.status==304){
				console.log(xhr.responseText+"dd\n");
				console.log(xhr.getAllResponseHeaders());
				alert(xhr.responseText);
			}else{
				alert("request was unsuccessful: "+xhr.status);
			}
		}
	}
	xhr.onprogress=function(event){
var divstatus=document.getElementById("s");
if(event.lengthComputable){
	divstatus.innerHTML="Received "+event.position+"of"+event.totalSize+"bytes";
}
	};
	xhr.open("post","http://xxxx",false);
	xhr.send();


	</script>

</head>
<body>
<div id="s">ddd</div>
</body>
</html>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章