關于ajax的特性,這里不再寫了,去網上隨便就能找到好多. 現在寫一個用ajax和jsp來實現的動態菜單的實例.先感覺一下ajax是如何實現傳說中的異步操作!
menu.html
<html>
<head>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<LINK href="images/css.css" type=text/css rel=stylesheet>
<script language="javascript">
var XMLHttpReq;
var currentSort;
//創建XMLHttpRequest對象
function createXMLHttpRequest() {
if(window.XMLHttpRequest) { //Mozilla 瀏覽器
XMLHttpReq = new XMLHttpRequest();
}
else if (window.ActiveXObject) { // IE瀏覽器
try {
XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
}
//發送請求函數
function sendRequest(url) {
createXMLHttpRequest();
XMLHttpReq.open("GET", url, true);
XMLHttpReq.onreadystatechange = processResponse;//指定響應函數
XMLHttpReq.send(null); // 發送請求
}
// 處理返回信息函數
function processResponse() {
if (XMLHttpReq.readyState == 4) { // 判斷對象狀態
if (XMLHttpReq.status == 200) { // 信息已經成功返回,開始處理信息
updateMenu();
} else { //頁面不正常
alert("您所請求的頁面有異常。");
}
}
}
//更新菜單函數
function updateMenu() {
var res=XMLHttpReq.responseXML.getElementsByTagName("res");
var subMenu = "";
for(var i = 0; i < res.length; i++) {
subMenu = subMenu + " " + res[i].lastChild.data;
}
currentSort.innerHTML = subMenu;
}
// 創建級聯菜單函數
function showSubMenu(obj) {
currentSort = document.getElementById(obj);
currentSort.parentNode.style.display = "";
sendRequest("menu.jsp?sort=" + obj);
}
</script>
</head>
<body>
<table style="BORDER-COLLAPSE: collapse" borderColor=#111111
cellSpacing=0 cellPadding=0 width=200 bgColor=#f5efe7 border=0>
<TR>
<TD align=middle bgColor=#dbc2b0 height=19><B>指標操作</B> </TD>
</TR>
<tr>
<td height="20"> <a onClick="showSubMenu('A')">一級指標A</a> </td>
</tr>
<tr style="display:none">
<td height="20" id="A"> </td>
</tr>
<tr>
<td height="20"> <a onClick="showSubMenu('B')">一級指標B</a> </td>
</tr>
<tr style="display:none ">
<td height="20" id="B"> </td>
</tr>
</table>
</body>
</html>
menu.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%
//接收瀏覽器端提交的信息
String sort=request.getParameter("sort");
String contents1="";
String contents2="";
if(sort.equals("A")){
contents1="一級指標AA";
contents2="一級指標AB";
}else if(sort.equals("B")){
contents1="二級指標BA";
contents2="二級指標BB";
}
//傳回響應數據
response.setContentType("text/xml; charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
out.println("<response>");
out.println("<res>" + contents1 + "</res>");
out.println("<res>" + contents2 + "</res>");
out.println("</response>");
out.close();
%>
OK , 代碼就這么多,十分簡單.
下面簡單介紹一下上面的ajax操作:
<!--[if !supportLists]-->1. <!--[endif]--> ajax技術主要由四部分組成
javascript
css
dom
XMLHttpRequest
<!--[if !supportLists]-->2. <!--[endif]--> ajax的核心對象
XMLHttpRequest
<!--[if !supportLists]-->3. <!--[endif]--> XMLHttpRequest對象的方法
void open(String method, String url, boolean async)
void send(String body)
void setHeader(String header, String value)
String getResponseHeader(String header)
String getAllResponseHeaders()
void abort()
<!--[if !supportLists]-->4. <!--[endif]-->XMLHttpRequest對象的屬性
<!--[if !supportLists]-->5. <!--[endif]-->ajax 操作的主要步驟
⑴ 聲明一個XMLHttpRequest對象.
⑵ 調用window.open(method, url , boolean),執行后臺操作
⑶ 對返回值進行操作, 一般用XMLHttpRequest對象的onreadystatechange屬性.
⑷ 執行send(body)方法.
新聞熱點
疑難解答
圖片精選