加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ajax

发布时间:2020-12-15 21:51:10 所属栏目:百科 来源:网络整理
导读:!DOCTYPEhtmlhtmllang="en"headmetacharset="UTF-8"titleajax/titlescript/***onreadystatechange每次状态改变所触发事件的事件处理程序。* responseText从服务器进程返回数据的字符串形式。* responseXML从服务器进程返回的DOM兼容的文档数据对象。* status
<!DOCTYPEhtml>
<htmllang="en">
<head>
	<metacharset="UTF-8">
	<title>ajax</title>
	<script>
/**
*onreadystatechange每次状态改变所触发事件的事件处理程序。
*  responseText从服务器进程返回数据的字符串形式。
*  responseXML从服务器进程返回的DOM兼容的文档数据对象。
*  status从服务器返回的数字代码,比如常见的404(未找到)和200(已就绪)
*  statusText伴随状态码的字符串信息
*  readyState对象状态值
 *   0(未初始化)对象已建立,但是尚未初始化(尚未调用open方法)
 *   1(初始化)对象已建立,尚未调用send方法
 *   2(发送数据)send方法已调用,但是当前的状态及http头未知
 *   3(数据传送中)已接收部分数据,因为响应及http头不全,这时通过responseBody和responseText获取部分数据会出现错误,
 *   4(完成)数据接收完毕,此时可以通过通过responseXml和responseText获取完整的回应数据
*/

functioncreateXHR(){
	if(typeofXMLHttpRequest!="undefined"){
		returnnewXMLHttpRequest();
	}elseif(typeofActiveXObject!="undefined"){
if(typeofarguments.callee.activeXString!="string"){
		varversions=["MSXML2.XMLHttp.6.0","MSXML2.XMLHttp.3.0","MSXML2.XMLHttp"],i,len;
			for(i=0,len=versions.length;i<len;i++){
				try{
					newActiveXObject(versions[i]);
					arguments.callee.activeXString=versions[i];
					break;
				}catch(ex){
					console.log("error:"+ex);
				}
			};

}
returnnewActiveXObject(arguments.callee.activeXString);
	}else{
		thrownewError("NoXHRobjectavailable!");
	}
}
	varxhr=createXHR();
	xhr.onreadystatechange=function(){
		if(xhr.readyState==4){
			if((xhr.status>=200&&xhr.status<300)||xhr.status==304){
				console.log(xhr.responseText+"ddn");
				console.log(xhr.getAllResponseHeaders());
				alert(xhr.responseText);
			}else{
				alert("requestwasunsuccessful:"+xhr.status);
			}
		}
	}
	xhr.onprogress=function(event){
vardivstatus=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>
<divid="s">ddd</div>
</body>
</html>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读