Ajax
AJAX
与服务器交换数据并更新部分网页的艺术,在不重新加载整个页面的情况下
是一种用于创建快速动态网页的技术。
通过在后台与服务器进行少量数据交换, 可以使网页实现异步更新。这意味着可以在不重新加载整个网页的情况下,对网页的某部分进行更新。 有很多使用 AJAX 的应用程序案例:新浪微博、Google 地图、开心网等等。 Google 通过其 Google Suggest 使 AJAX 变得流行起来。 Google Suggest 使用 AJAX 创造出动态性极强的 web 界面:当您在谷歌的搜索框输入关键字时,JavaScript 会把这些字符发送到服务器,然后服务器会返回一个搜索建议的列表
AJAX 的工作原理
XMLHttpRequest 对象
XMLHttpRequest 用于在后台与服务器交换数据。这意味着可以在不重新加载整个网页的情况下,对网页的某部分进行更新
创建 XMLHttpRequest 对象语法:variable=new XMLHttpRequest();
请检查浏览器是否支持 XMLHttpRequest 对象。如果支持,则创建 XMLHttpRequest 对象。如果不支持,则创建 ActiveXObject : var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+,Firefox,Chrome,Opera,Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6,IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
XMLHttpRequest 对象用于和服务器交换数据。
向服务器发送请求如需将请求发送到服务器,我们使用 XMLHttpRequest 对象的 open() 和 send() 方法: xmlhttp.open("GET","test1.txt",true); xmlhttp.send(); 与 POST 相比,GET 更简单也更快,并且在大部分情况下都能用,在以下情况中,请使用 POST 请求:
使用 XMLHttpRequest 对象的 open() 和 send() 方法: xmlhttp.open("GET",true); //get方法,异步传输,url=test1.txt xmlhttp.send(); POST 请求请使用 setRequestHeader() 来添加 HTTP 头。然后在 send() 方法中规定您希望发送的数据: xmlhttp.open("POST","ajax_test.asp",true); xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xmlhttp.send("fname=Bill&lname=Gates");
通过 AJAX,JavaScript 无需等待服务器的响应,而是:
实例:
<head>
<script type="text/javascript"> function loadXMLDoc() { var xmlhttp; //判定浏览器 if(window.XMLHttpRequest){ xmlhttp = new XMLHttpRequest(); }else{ xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } //执行 xmlhttp.onreadystatechange = function(){ if(xmlhttp.readyState == 4 && xmlhttp.status == 200){ document.getElementById("myDiv").innerHTML = xmlhttp.responseText; } } xmlhttp.open("GET","/ajax/test1.php",true); xmlhttp.send(); // document.getElementById("myDiv").innerHTML = xmlhttp.responseText; } </script> </head> <body> <button type="button" onclick="loadXMLDoc()">请求数据</button> <div id="myDiv"></div> </body> Async = true当使用 async=true 时,请规定在响应处于 onreadystatechange 事件中的就绪状态时执行的函数: xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("myDiv").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET",true); xmlhttp.send();
注释:当您使用
async=false时,请不要编写 onreadystatechange 函数 - 把代码放到 send() 语句后面即可:
xmlhttp.open("GET",false); xmlhttp.send(); document.getElementById("myDiv").innerHTML=xmlhttp.responseText; 服务器响应
获得来自服务器的响应,请使用 XMLHttpRequest 对象的 responseText 或 responseXML 属性
responseText 属性responseText 属性返回字符串形式的响应可以这样使用: document.getElementById("myDiv").innerHTML=xmlhttp.responseText; responseXML 属性
来自服务器的响应是 XML,而且需要作为 XML 对象进行解析
xmlDoc=xmlhttp.responseXML; txt=""; x=xmlDoc.getElementsByTagName("ARTIST"); for (i=0;i<x.length;i++) { txt=txt + x[i].childNodes[0].nodeValue + "<br />"; } document.getElementById("myDiv").innerHTML=txt; onreadystatechange 事件当请求被发送到服务器时,我们需要执行一些基于响应的任务。 每当 readyState 改变时,就会触发 onreadystatechange 事件。 readyState 属性存有 XMLHttpRequest 的状态信息 XMLHttpRequest 对象的三个重要的属性:
使用 Callback 函数
callback 函数是一种以参数形式传递给另一个函数的函数
function myFunction() { loadXMLDoc("ajax_info.txt",function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("myDiv").innerHTML=xmlhttp.responseText; } }); }
AJAX 可用来与 XML 文件进行交互式通信
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |