AJAX的使用步骤

1创建XMLHttpRequest 
</head>
<script type="text/javascript" >
      var xmlHttp = null
      //创建XMLHttpRequest对像                                                  
      function createXMLHttpRequest() {                                        
            if (window.XMLHttpRequest) {//如果为其他浏览器                        
                  xmlHttp = new XMLHttpRequest();                              
            } else if (window.ActiveXObject) { //如果为IE                       
                   try {                                                       
                        xmlHttp = new ActiveXObject("Msxml2.XMLHTTP" );        
                  } catch (e) {                                                
                         try {                                                 
                              xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                        } catch (ex) {                                         
                        }                                                      
                  }                                                            
            }                                                                  
      }                                                                        
       function sendRequest(url) {                                             
             if (xmlHttp == null) {                                            
                  createXMLHttpRequest();                                      
            }                                                                  
            xmlHttp.onreadystatechange = callback;                             
            xmlHttp.open( "GET", url, true);                                   
            xmlHttp.send( null);                                               
      }                                                                        
       function callback() {                                                   
             if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {           
                   var name = xmlHttp.responseText;                            
                   var d1 = document.getElementById("d1");                     
                  d1.innerHTML = name;                                         
            }                                                                  
      }                                                                        
       function sayHello() {                                                   
             var userName = document.getElementById("userName").value;         
             var url = "testAJAX?userName=" + userName;                        
            sendRequest(url);                                                  
      }                                                                        
</ script>                                                                     
<body>
       <div id= "d1"></div >
       <input type="text" id="userName" />
       <input type="button" value="hai" onclick="sayHello();" />
</body>
</html>
posted on 2012-11-24 21:30  YangJin  阅读(191)  评论(0)    收藏  举报