论坛首页 入门技术论坛

一个简单的AJAX例子

浏览 1915 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-11-26  
xml 代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"   
  2.   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head>  
  5. <title>Simple XMLHttpRequest</title>  
  6.       
  7. <script type="text/javascript">  
  8. var xmlHttp;   
  9.   
  10. function createXMLHttpRequest() {   
  11.     if (window.ActiveXObject) {   
  12.         xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");   
  13.     }   
  14.     else if (window.XMLHttpRequest) {   
  15.         xmlHttp = new XMLHttpRequest();   
  16.     }   
  17. }   
  18.       
  19. function startRequest() {   
  20.     createXMLHttpRequest();   
  21.     xmlHttp.onreadystatechange = handleStateChange;   
  22.     xmlHttp.open("GET", "simpleResponse.xml", true);   
  23.     xmlHttp.send(null);   
  24. }   
  25.       
  26. function handleStateChange() {   
  27.     if(xmlHttp.readyState == 4) {   
  28.         if(xmlHttp.status == 200) {   
  29.             alert("The server replied with: " + xmlHttp.responseText);   
  30.         }   
  31.   
  32. }   
  33. }   
  34. </script>  
  35. </head>  
  36.   
  37. <body>  
  38.     <form action="#">  
  39.         <input type="button" value="Start Basic Asynchronous Request" onclick="startRequest();"/>  
  40.     </form>  
  41. </body>  
  42. </html>  
  43.   
  44. 所用到的xml文件   
  45.   
  46. simpleResponse.xml//放在同一个目录下就可以了   
  47.   
  48. Hello from the server!   
  49.     
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics