1 <script type="text/javascript">
2 window.onload = function () {
3 document.getElementById("time").onclick = function () {
4 //向服务器请求时间
5 //1. 创建异步对象(相当与一个小浏览器)
6 var xhr = new XMLHttpRequest();
7 //2. 设置参数(请求方式,请求哪个页面,是否异步)
8 xhr.open("get", "Ajax.ashx", true);
9 //如果不想让浏览器不拿缓存数据,每次请求都会到服务器拿数据,如下设置
10 xhr.setRequestHander("If-Modified-Since","0");
11 //3. 设置回调函数
12 xhr.onreadystatechange = function () {
13 if (xhr.readyState == 4 && xhr.status == 200) {
14 var res = xhr.responseText;
15 alert(res);
16 }
17 }
18
19 //4. 发送异步请求
20 xhr.send(null);
21 }
22 }
23 </script>