一、Ajax 简介
Ajax = 异步的 javascript 和 xml;
Ajax是一种用于快速创建动态网页的技术。原理图如下:
二、Demo
1: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2: <html xmlns="http://www.w3.org/1999/xhtml">
3: <head>
4: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5: <title>ajax</title>
6: <script type="text/javascript">
7:
8:
9: function loadDoc() {
10: var xmlhttp;
11: if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
12: xmlhttp = new XMLHttpRequest();
13: } else {// code for IE6, IE5
14: xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
15: }
16: xmlhttp.onreadystatechange = function() {
17: if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
18: document.getElementById("text").innerHTML = xmlhttp.responseText;
19: }
20: }
21: xmlhttp.open("get", "/ajaxTest/text.txt", true);
22: xmlhttp.send();
23: }
24:
25: </script>
26: </head>
27:
28: <body>
29: <div id="text">Ajax can change these text<
30: <button onclick="loadDoc()" value="change">change</button>
31: </body>
32: </html>
33:
34: //text.txt 内容可以自己随意编写,只需保证其在路径下就行了。
解释如下:
1、XMLHttpRequest 对象XMLHttpRequest 用于在后台与服务器交换数据。这意味着可以在不重新加载整个网页的情况下,对网页的某部分进行更新。
几乎所有所有浏览器(IE7+、Firefox、Chrome、Safari 以及 Opera)都支持 XMLHttpRequest 对象,IE5 和 IE6 支持 ActiveXObject。为了兼容所有浏览器,一般会如下建立 XMLHttpRequest 对象,
1: var xmlhttp;
2: if (window.XMLHttpRequest) {// IE7+, Firefox, Chrome, Opera, Safari
3: xmlhttp = new XMLHttpRequest();
4: } else {// IE6, IE5
5: xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
6: }
7:
2、XMLHttpRequest 对象向服务器发送请求
可以使用 XMLHttpRequest 对象的 open()或者 send()方法向服务器其发送请求。如下:
1: xmlhttp.open("get", "/ajaxTest/text.txt", true);
2: xmlhttp.send();
方法 | 描述 |
---|---|
open(method,url,async) | 规定请求的类型、URL 以及是否异步处理请求。
|
send(string) | 将请求发送到服务器。
|
既然无妨通过send 方法添加参数,那该如何给 get 方法发送信息呢??可以向 url 添加信息,如下:
1: xmlhttp.open("get","index.jsp?name=Mike&password=123456",true);
2: xmlhttp.send();
3、GET 还是 POST?
与 POST 相比,GET 更简单也更快,并且在大部分情况下都能用。
然而,在以下情况中,请使用 POST 请求:
-
- 无法使用缓存文件(更新服务器上的文件或数据库)
- 向服务器发送大量数据(POST 没有数据量限制)
- 发送包含未知字符的用户输入时,POST 比 GET 更稳定也更可靠
4、post 请求
1、简单的post 请求
1: xmlhttp.open("post","index.jsp",true);
2: xmlhttp.send();
可以采用类似于 html 的形式来发送 post 请求,使用 setRequestHeader()添加 http 头,使用send()方法来发送数据,如下:
1: xmlhttp.open("post","index.jsp",true);
2: xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
3: xmlhttp.send("name=Mike&password=123456");
5、服务器响应
可以使用 XMLHttpRequest 对象的 responseText 或 responseXML 来获得服务器响应的数据。当中 responseText 用来获取文本数据,responseXML 用来xml数据。
Demo1:
1: document.getElementById("text").innerHTML=xmlhttp.responseText;
Demo2:
1: xmlDoc=xmlhttp.responseXML;
2: txt="";
3: x=xmlDoc.getElementsByTagName("name");
4: for (i=0;i<x.length;i++)
5: {
6: txt=txt + x[i].childNodes[0].nodeValue + "<br />";
7: }
8: document.getElementById("text").innerHTML=txt;
6、onreadystatechange 事件
(1)当请求被发送到服务器时,我们需要执行一些基于响应的任务。每当 readyState 改变时,就会触发 onreadystatechange 事件。readyState 属性存有 XMLHttpRequest 的状态信息。下面是 XMLHttpRequest 对象的三个重要的属性:
属性 | 描述 |
---|---|
onreadystatechange | 存储函数(或函数名),每当 readyState 属性改变时,就会调用该函数。 |
readyState | 存有 XMLHttpRequest 的状态。从 0 到 4 发生变化。
|
status | 200: "OK" 404: 未找到页面 |
当 readyState 为 4,status 为 200时,表示响应已就绪(可以处理从服务器返回的 responseText 或 responseXML,如下:
1: xmlhttp.onreadystatechange = function() {
2: if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
3: document.getElementById("text").innerHTML = xmlhttp.responseText;
4: }
5: }
(2)使用 callBack 函数
callback 函数是一种以参数形式传递给另一个函数的函数。如果您的网站上存在多个 AJAX 任务,那么您应该为创建 XMLHttpRequest 对象编写一个标准的函数,并为每个 AJAX 任务调用该函数,
1: function httpRequest(reqType,url,async,callBack,parameter){
2: var xmlhttp = null;
3: if(window.XMLHttpRequest){
4: xmlhttp = new XMLHttpRequest();
5: }else if(window.ActiveXObject){ //IE5,IE6浏览器
6: xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
7: }
8: xmlhttp .open(reqType, url, true); //打开服务器连接
9: //设置请求头
10: xmlhttp .setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
11: xmlhttp .onreadystatechange = callBack;//设置处理响应的回调函数
12: parameter = encodeURI(parameter); //将参数字符串进行编码
13: xmlhttp.send(parameter); //发送请求
14:
15: return xmlhttp;
16: }
17:
18: function callBack(){
19:
20: }