ajax简介

ajax 异步javascript和xml(Asynchronous Javascript And XML)

用于在后台和服务器进行少量的数据交换

我们可以不用重新加载整个网页的情况下改变局部网页

直接贴代码

<html>
<head>
	<title>this is ajax test</title>
	<script type="text/javascript">
		function getXMLHttpRequest(){
			try{
				try{
					return new ActiveXObject("Microsoft.XMLHTTP");
				}catch(e){
					return new ActiveXObject("Msxml2.XMLHTTP");
				}
			}catch(e){
				return new XMLHttpRequest();
			}
		}

		var myRequest = new getXMLHttpRequest();

		function callAjax(){
			//build a url we wish to call
			var url = "1.php";
			//ask out XMLHttpRequest to open a server connection
			myRequest.open("GET",url,true);
			//prepare afunction responseAjax() to run when the response has arrived
			myRequest.onreadystatechange = responseAjax;
			//finally send the request
			myRequest.send(null);
		}

		function responseAjax(){

			if(myRequest.readyState ==  4){
				if(myRequest.status == 200){
					//if our request is normal then we prompt the information we got
					alert("the server said:"+myRequest.responseText);
				}else{
					alert("something wrong is happened with the server!");
				}
			}
		}
	</script>
</head>
<body>
	<input type="button" value="get text" onclick="callAjax()"></input>
</body>
</html>

首先构造一个XMLHttpRequest对象,用这个对象打开域内连接,等待服务器返回数据,myRequest.onreadystatechane=true就会调用respoonseAjax方法,在方法体中我们处理返回来的信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值