ajax原理

ajax

<!DOCTYPE html>
	    <html lang="en">
	    <head>
		<meta charset="UTF-8">
		<title>ajax原理</title>
	</head>
	<body>
		<input type="text" id="username">
		<div id="use1"></div>
	</body>
		<script>
			function CreateXmlHttp(){
				//非IE浏览器创建XmlHttpRequest对象
				if(window.XMLHttpRequest){
					xmlhttp=new XMLHttpRequest();
				}
				//IE浏览器器创建XmlHttpRequest对象
				if(window.ActiveXObject){
					try{
						xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
					}catch(e){
						try{
							xmlhttp=new ActiveXObject("msxm12.XMLHTTP");
						}catch(ex){}
					}
				}
			}
			function Ustbwuyi(){
				var data=document.getElementById('username').value;
				CreateXmlHttp();
				if(!xmlhttp){
					alert("创建xmlhttp对象异常");
					return false;
				}
				xmlhttp.open("get","/postData",false);
				xmlhttp.onreadystatechange=function(){
					if(xmlhttp.readyState==4){
						document.getElementById("use1").innerHTML="数据正在加载...";
						if(xmlhttp.status==200){
							console.log(xmlhttp.responseText);
						}
					}
				}
				xmlhttp.send();
			}
			Ustbwuyi();
		</script>
	</html>
posted @ 2017-11-03 12:56  不完美的完美  阅读(136)  评论(0编辑  收藏  举报