当前位置: 代码迷 >> Ajax >> 一个容易的AJAX例子
  详细解决方案

一个容易的AJAX例子

热度:284   发布时间:2012-11-09 10:18:47.0
一个简单的AJAX例子
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. ? ??
  相关解决方案