[关闭]
@guoxs 2015-12-18T13:37:36.000000Z 字数 3471 阅读 2209

再探Ajax

Ajax


AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML)

1、XMLHttpRequest对象

所有现代浏览器均支持 XMLHttpRequest 对象(IE5 和 IE6 使用 ActiveXObject)。
XMLHttpRequest 用于在后台与服务器交换数据。这意味着可以在不重新加载整个网页的情况下,对网页的某部分进行更新。

2、创建XMLHttpRequest 对象

  1. var xmlhttp;
  2. if (window.XMLHttpRequest)
  3. {// code for IE7+, Firefox, Chrome, Opera, Safari
  4. xmlhttp=new XMLHttpRequest();
  5. }
  6. else
  7. {// code for IE6, IE5
  8. xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  9. }

3、向服务器发送请求

XMLHttpRequest 对象用 open() 和 send() 方法像服务器发送请求。

  1. xmlhttp.open("GET","test1.txt",true);
  2. xmlhttp.send();
方法 描述
open(method,url,async) 规定请求的类型、URL 以及是否异步处理请求。method:请求的类型;GET 或 POST; url:文件在服务器上的位置; async:true(异步)或 false(同步)
send(string) 将请求发送到服务器。string:仅用于 POST 请求

与 POST 相比,GET 更简单也更快,并且在大部分情况下都能用。
然而,在以下情况中,请使用 POST 请求:

4、GET 方法

  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function loadXMLDoc(){
  5. var xmlhttp;
  6. if (window.XMLHttpRequest){
  7. // code for IE7+, Firefox, Chrome, Opera, Safari
  8. xmlhttp=new XMLHttpRequest();
  9. }else{
  10. // code for IE6, IE5
  11. xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  12. }
  13. xmlhttp.onreadystatechange=function(){
  14. if (xmlhttp.readyState==4 && xmlhttp.status==200){
  15. document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
  16. }
  17. }
  18. xmlhttp.open("GET","/ajax/demo_get.asp",true);
  19. xmlhttp.send();
  20. }
  21. </script>
  22. </head>
  23. <body>
  24. <h2>AJAX</h2>
  25. <button type="button" onclick="loadXMLDoc()">请求数据</button>
  26. <div id="myDiv"></div>
  27. </body>
  28. </html>

若使用

  1. xmlhttp.open("GET","demo_get.asp",true);
  2. xmlhttp.send();

可能会得到缓存的结果,可以向 URL 添加一个唯一的 ID避免:

  1. xmlhttp.open("GET","demo_get.asp?t=" + Math.random(),true);
  2. xmlhttp.send();

使用GET方法发送信息:

  1. xmlhttp.open("GET","demo_get2.asp?fname=Bill&lname=Gates",true);
  2. xmlhttp.send();

5、POST 请求

  1. xmlhttp.open("POST","demo_post.asp",true);
  2. xmlhttp.send();

如果 POST HTML 表单数据,可以使用 setRequestHeader() 来添加 HTTP 头。然后在 send() 方法中规定希望发送的数据:

  1. xmlhttp.open("POST","ajax_test.asp",true);
  2. xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
  3. xmlhttp.send("fname=Bill&lname=Gates");

6、Async

使用异步方法:

  1. xmlhttp.open("GET","ajax_test.asp",true);

使用 async=true 时,需要规定在响应处于 onreadystatechange 事件中的就绪状态时执行的函数:

  1. xmlhttp.onreadystatechange=function()
  2. {
  3. if (xmlhttp.readyState==4 && xmlhttp.status==200)
  4. {
  5. document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
  6. }
  7. }
  8. xmlhttp.open("GET","test1.txt",true);
  9. xmlhttp.send();

如使用 async=false,open() 方法中的第三个参数改为 false:

  1. xmlhttp.open("GET","test1.txt",false);

使用 async=false 时,不要编写 onreadystatechange 函数, 把代码放到 send() 语句后面即可:

  1. xmlhttp.open("GET","test1.txt",false);
  2. xmlhttp.send();
  3. document.getElementById("myDiv").innerHTML=xmlhttp.responseText;

7、获取服务器响应

responseText: 获得字符串形式的响应数据。
responseXML: 获得 XML 形式的响应数据。

responseText 属性返回字符串形式的响应,因此可以这样使用:

  1. document.getElementById("myDiv").innerHTML=xmlhttp.responseText;

如果来自服务器的响应是 XML,而且需要作为 XML 对象进行解析,使用 responseXML 属性:

  1. xmlDoc=xmlhttp.responseXML;
  2. txt="";
  3. x=xmlDoc.getElementsByTagName("ARTIST");
  4. for (i=0;i<x.length;i++)
  5. {
  6. txt=txt + x[i].childNodes[0].nodeValue + "<br />";
  7. }
  8. document.getElementById("myDiv").innerHTML=txt;

8、onreadystatechange 事件

每当 readyState 改变时,就会触发 onreadystatechange 事件。
readyState 属性存有 XMLHttpRequest 的状态信息。

onreadystatechange 存储函数(或函数名),每当 readyState 属性改变时,就会调用该函数。
readyState 存有 XMLHttpRequest 的状态。从 0 到 4 发生变化。

0: 请求未初始化
1: 服务器连接已建立
2: 请求已接收
3: 请求处理中
4: 请求已完成,且响应已就绪

status

200: "OK"
404: 未找到页面

当 readyState 等于 4 且状态为 200 时,表示响应已就绪。

Callback 函数
callback 函数是一种以参数形式传递给另一个函数的函数。

如果存在多个 AJAX 任务,应该为创建 XMLHttpRequest 对象编写一个标准的函数,并为每个 AJAX 任务调用该函数。 该函数调用应该包含 URL 以及发生 onreadystatechange 事件时执行的任务(每次调用可能不尽相同):

  1. function myFunction()
  2. {
  3. loadXMLDoc("ajax_info.txt",function()
  4. {
  5. if (xmlhttp.readyState==4 && xmlhttp.status==200)
  6. {
  7. document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
  8. }
  9. });
  10. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注