代码改变世界

今天关注了下AJAX,写了一个入门级的例子

2005-10-22 13:31  sun@live  阅读(450)  评论(2编辑  收藏  举报

HTTP 请求:

<script type="text/javascript" language="javascript">

   var http_request = false; 
   function makeRequest(url) { 
       if (window.XMLHttpRequest) { //mozilla...浏览器
           http_request = new XMLHttpRequest();
       } else if (window.ActiveXObject) { // IE 浏览器
           http_request = new ActiveXObject("Microsoft.XMLHTTP");
       }         
       http_request.onreadystatechange = alertContents; //事件
       http_request.open('GET', url, true); //以GET方式发送请求
       http_request.send(null);

   }     
   function alertContents() { 
       if (http_request.readyState == 4) {
           if (http_request.status == 200) { //请求正确收到
                //在请求正确收到后用消息框显示内容
               alert(http_request.responseText);
           } else { //请求有错
               alert('There was a problem with the request.');
           }
       }
   }
</script>
<a href="#" onclick="makeRequest('test.html')">Make a request</a>