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方法,在方法体中我们处理返回来的信息。