AJAX 实例

本例演示与 XML 一起使用的 JavaScript (AJAX)。

使用 XMLHttpRequest 对象的实例

通过 XML HTTP 把文本文件载入 HTML 元素

  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. var xmlhttp;
  5. function loadXMLDoc(url)
  6. {
  7. xmlhttp=null;
  8. if (window.XMLHttpRequest)
  9. {// code for Firefox, Opera, IE7, etc.
  10. xmlhttp=new XMLHttpRequest();
  11. }
  12. else if (window.ActiveXObject)
  13. {// code for IE6, IE5
  14. xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  15. }
  16. if (xmlhttp!=null)
  17. {
  18. xmlhttp.onreadystatechange=state_Change;
  19. xmlhttp.open("GET",url,true);
  20. xmlhttp.send(null);
  21. }
  22. else
  23. {
  24. alert("Your browser does not support XMLHTTP.");
  25. }
  26. }
  27. function state_Change()
  28. {
  29. if (xmlhttp.readyState==4)
  30. {// 4 = "loaded"
  31. if (xmlhttp.status==200)
  32. {// 200 = "OK"
  33. document.getElementById('T1').innerHTML=xmlhttp.responseText;
  34. }
  35. else
  36. {
  37. alert("Problem retrieving data:" + xmlhttp.statusText);
  38. }
  39. }
  40. }
  41. </script>
  42. </head>
  43. <body onload="loadXMLDoc('/example/ajax/test_xmlhttp.txt')">
  44. <div id="T1" style="border:1px solid black;height:40;width:300;padding:5"></div><br />
  45. <button onclick="loadXMLDoc('/example/ajax/test_xmlhttp2.txt')">Click</button>
  46. </body>
  47. </html>

通过 XML HTTP 加载 XML 文件

  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. var xmlhttp;
  5. function loadXMLDoc(url)
  6. {
  7. xmlhttp=null;
  8. if (window.XMLHttpRequest)
  9. {// code for IE7, Firefox, Opera, etc.
  10. xmlhttp=new XMLHttpRequest();
  11. }
  12. else if (window.ActiveXObject)
  13. {// code for IE6, IE5
  14. xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  15. }
  16. if (xmlhttp!=null)
  17. {
  18. xmlhttp.onreadystatechange=state_Change;
  19. xmlhttp.open("GET",url,true);
  20. xmlhttp.send(null);
  21. }
  22. else
  23. {
  24. alert("Your browser does not support XMLHTTP.");
  25. }
  26. }
  27. function state_Change()
  28. {
  29. if (xmlhttp.readyState==4)
  30. {// 4 = "loaded"
  31. if (xmlhttp.status==200)
  32. {// 200 = "OK"
  33. document.getElementById('A1').innerHTML=xmlhttp.status;
  34. document.getElementById('A2').innerHTML=xmlhttp.statusText;
  35. document.getElementById('A3').innerHTML=xmlhttp.responseText;
  36. }
  37. else
  38. {
  39. alert("Problem retrieving XML data:" + xmlhttp.statusText);
  40. }
  41. }
  42. }
  43. </script>
  44. </head>
  45. <body>
  46. <h2>Using the HttpRequest Object</h2>
  47. <p><b>Status:</b>
  48. <span id="A1"></span>
  49. </p>
  50. <p><b>Status text:</b>
  51. <span id="A2"></span>
  52. </p>
  53. <p><b>Response:</b>
  54. <br /><span id="A3"></span>
  55. </p>
  56. <button onclick="loadXMLDoc('/example/xmle/note.xml')">Get XML</button>
  57. </body>
  58. </html>

通过 XML HTTP 进行一次 HEAD 请求

  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. var xmlhttp;
  5. function loadXMLDoc(url)
  6. {
  7. xmlhttp=null;
  8. if (window.XMLHttpRequest)
  9. {// code for Firefox, Mozilla, IE7, etc.
  10. xmlhttp=new XMLHttpRequest();
  11. }
  12. else if (window.ActiveXObject)
  13. {// code for IE6, IE5
  14. xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  15. }
  16. if (xmlhttp!=null)
  17. {
  18. xmlhttp.onreadystatechange=state_Change;
  19. xmlhttp.open("GET",url,true);
  20. xmlhttp.send(null);
  21. }
  22. else
  23. {
  24. alert("Your browser does not support XMLHTTP.");
  25. }
  26. }
  27. function state_Change()
  28. {
  29. if (xmlhttp.readyState==4)
  30. {// 4 = "loaded"
  31. if (xmlhttp.status==200)
  32. {// 200 = "OK"
  33. document.getElementById('p1').innerHTML=xmlhttp.getAllResponseHeaders();
  34. }
  35. else
  36. {
  37. alert("Problem retrieving data:" + xmlhttp.statusText);
  38. }
  39. }
  40. }
  41. </script>
  42. </head>
  43. <body>
  44. <p id="p1">
  45. The getAllResponseHeaders() function returns the headers of a resource.
  46. The headers contain file information like length,
  47. server-type, content-type, date-modified, etc.</p>
  48. <button onclick="loadXMLDoc('/example/ajax/test_xmlhttp.txt')">Get Headers</button>
  49. </body>
  50. </html>

通过 XML HTTP 进行一次指定的 HEAD 请求

  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. var xmlhttp;
  5. function loadXMLDoc(url)
  6. {
  7. xmlhttp=null;
  8. if (window.XMLHttpRequest)
  9. {// all modern browsers
  10. xmlhttp=new XMLHttpRequest();
  11. }
  12. else if (window.ActiveXObject)
  13. {// for IE5, IE6
  14. xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  15. }
  16. if (xmlhttp!=null)
  17. {
  18. xmlhttp.onreadystatechange=state_Change;
  19. xmlhttp.open("GET",url,true);
  20. xmlhttp.send(null);
  21. }
  22. else
  23. {
  24. alert("Your browser does not support XMLHTTP.");
  25. }
  26. }
  27. function state_Change()
  28. {
  29. if (xmlhttp.readyState==4)
  30. {// 4 = "loaded"
  31. if (xmlhttp.status==200)
  32. {// 200 = "OK"
  33. document.getElementById('p1').innerHTML="This file was last modified on: " + xmlhttp.getResponseHeader('Last-Modified');
  34. }
  35. else
  36. {
  37. alert("Problem retrieving data:" + xmlhttp.statusText);
  38. }
  39. }
  40. }
  41. </script>
  42. </head>
  43. <body>
  44. <p id="p1">
  45. The getResponseHeader() function returns a header from a resource.
  46. Headers contain file information like length,
  47. server-type, content-type, date-modified, etc.</p>
  48. <button onclick="loadXMLDoc('/example/ajax/test_xmlhttp.txt')">Get "Last-Modified"</button>
  49. </body>
  50. </html>

把 XML 文件显示为 HTML 表格

  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. var xmlhttp;
  5. function loadXMLDoc(url)
  6. {
  7. xmlhttp=null;
  8. if (window.XMLHttpRequest)
  9. {// code for IE7, Firefox, Mozilla, etc.
  10. xmlhttp=new XMLHttpRequest();
  11. }
  12. else if (window.ActiveXObject)
  13. {// code for IE5, IE6
  14. xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  15. }
  16. if (xmlhttp!=null)
  17. {
  18. xmlhttp.onreadystatechange=onResponse;
  19. xmlhttp.open("GET",url,true);
  20. xmlhttp.send(null);
  21. }
  22. else
  23. {
  24. alert("Your browser does not support XMLHTTP.");
  25. }
  26. }
  27. function onResponse()
  28. {
  29. if(xmlhttp.readyState!=4) return;
  30. if(xmlhttp.status!=200)
  31. {
  32. alert("Problem retrieving XML data");
  33. return;
  34. }
  35. txt="<table border='1'>";
  36. x=xmlhttp.responseXML.documentElement.getElementsByTagName("CD");
  37. for (i=0;i<x.length;i++)
  38. {
  39. txt=txt + "<tr>";
  40. xx=x[i].getElementsByTagName("TITLE");
  41. {
  42. try
  43. {
  44. txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
  45. }
  46. catch (er)
  47. {
  48. txt=txt + "<td> </td>";
  49. }
  50. }
  51. xx=x[i].getElementsByTagName("ARTIST");
  52. {
  53. try
  54. {
  55. txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
  56. }
  57. catch (er)
  58. {
  59. txt=txt + "<td> </td>";
  60. }
  61. }
  62. txt=txt + "</tr>";
  63. }
  64. txt=txt + "</table>";
  65. document.getElementById('copy').innerHTML=txt;
  66. }
  67. </script>
  68. </head>
  69. <body>
  70. <div id="copy">
  71. <button onclick="loadXMLDoc('/example/xmle/cd_catalog.xml')">Get CD info</button>
  72. </div>
  73. </body>
  74. </html>

例子解释

当用户键入文本时,通过使用 XML HTTP 与服务器进行在线通信

  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. var xmlHttp=null;
  5. function showHint(str)
  6. {
  7. if (str.length==0)
  8. {
  9. document.getElementById("txtHint").innerHTML="";
  10. return;
  11. }
  12. try
  13. {// Firefox, Opera 8.0+, Safari, IE7
  14. xmlHttp=new XMLHttpRequest();
  15. }
  16. catch(e)
  17. {// Old IE
  18. try
  19. {
  20. xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  21. }
  22. catch(e)
  23. {
  24. alert ("Your browser does not support XMLHTTP!");
  25. return;
  26. }
  27. }
  28. var url="/ajax/gethint.asp?q=" + str;
  29. url=url+"&sid="+Math.random();
  30. xmlHttp.open("GET",url,false);
  31. xmlHttp.send(null);
  32. document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
  33. }
  34. </script>
  35. </head>
  36. <body><form>
  37. First Name:
  38. <input type="text" id="txt1"
  39. onkeyup="showHint(this.value)">
  40. </form><p>Suggestions: <span id="txtHint"></span></p> </body>
  41. </html>

例子解释