二、標準XMLHttpRequest操作
abort():停止當前請求;
getAllResponseHeaders():把HTTP請求的所有的響應(yīng)首部作為鍵/值對返回;
getResponseHeader(STring header):返回指定首部的串值;
open(String method,String url,boolean asynch,String username,String password):建立對服務(wù)器的調(diào)用,method可以是get,pos或put。url參數(shù)可以是相對url或絕對url,后面三個參數(shù)是可選的。asynch表示這個調(diào)用是異步的還是同步的,默認是true,表示請求本質(zhì)上是異步的,如果這個參數(shù)為false,處理就會等待,直到從服務(wù)器返回響應(yīng)為止。由于異步調(diào)用是使用Ajax的主要優(yōu)勢之一,所以倘若將這個參數(shù)設(shè)置為false,從某種程度上講與使用XMLHttpRequest對象的初衷不太相符。不過,在某些情況下設(shè)置這個參數(shù)為false也是有用的,比如在持久存儲頁面之前可以先驗證用戶的輸入。最后兩個參數(shù)允許指定一個特定的用戶名和密碼。
void send(content):這個方法具體想服務(wù)器發(fā)出請求,如果請求聲明為異步的,這個方法就會立即返回,否則它會等待直到結(jié)束到響應(yīng)為止。可選參數(shù)可以是DOM對象的實例,輸入流,或者串。傳入這個方法的內(nèi)容會作為請求體的一部分發(fā)送。
void setRequestHeader(String header,String value):這個方法為HTTP請求中一個給定的首部設(shè)置值。注意,這個方法必須在調(diào)用open()之后才能調(diào)用。
String getAllResponseHeaders():這個方法返回一個串,其中包含HTTP請求的所有首部,包括Content-Length、date和url。
三、標準XMLHttpRequest屬性
onreadystatechange:每個狀態(tài)改變時都會觸發(fā)這個事件器,通常會調(diào)用一個javaScript函數(shù);
readyState:請求的狀態(tài)。有5個可選值:0=未初始化,1=正在加載,2=已加載,3=交互中,4=完成;
responseText:服務(wù)器的響應(yīng),表示為一個串;
responseXMl:服務(wù)器的響應(yīng),表示為XML,這個對象可以解析為一個DOM對象;
status:服務(wù)器的HTTP狀態(tài)碼(200對應(yīng)OK,404對應(yīng)Not Found(未找到),等等);
statusText:HTTP狀態(tài)碼的相應(yīng)文本(OK或Not Found等等);
四、用于處理XML文檔的DOM元素屬性
childNodes:返回當前元素的所有子元素的數(shù)組;
firstChild:返回當前元素的第一個下級子元素;
lastChild:返回當前元素的最后一個子元素;
nextSibling:返回當前元素后面的元素(下一個兄弟);
nodeValue:指定表示元素值的讀/寫屬性;
parentNode:返回當前元素的父節(jié)點;
previousSibling:返回當前元素的前一個元素(前一個兄弟);
五、用于遍歷XML文檔的DOM元素方法
getElementById(id):獲取有唯一ID屬性值文檔中的元素;
getElementsByTagName(name):返回當前元素中有指定標記名的子元素的數(shù)組;
hasChildNodes():返回一個布爾值,表示元素是否還有子元素;
getAttribute(name):返回元素的屬性值,屬性有name指定;
六、動態(tài)創(chuàng)建內(nèi)容時常用到的DOM屬性和方法
document.createElement(tageName):創(chuàng)建由tagName指定的元素;
document.createTextNode(text):創(chuàng)建一個包含靜態(tài)文本的節(jié)點;
<element>.appendChild(childNode):給當前元素增加一個子節(jié)點;
<element>.getAttribute(name):獲取name屬性;
<element>.setAttribute(name,value):設(shè)置name屬性的值;
<element>.insertBefore(newNode,targetNode):把節(jié)點newNode作為當前元素的子節(jié)點插入帶targetNode元素的前面
<element>.removeAttribute(name):這個方法從元素中刪除屬性name;
<element>.removeChild(childNode):刪除子元素;
<element>.replaceChild(newNode,oldNode):替換節(jié)點;
七、判斷IE瀏覽器
IE能識別出名為uniqueID的document對象的專用屬性,IE也是唯一能識別這個屬性的瀏覽器,所以使用這個屬性可以確定瀏覽器,例如:
if(document.uniqueID){//IE
var radioButton=document.createElement("<input type='radio' name='radioButton' value='checked'>");
}
else{
var radioButton=document.createElement("input");
radioButton.setAttribute("type","radio");
radioButton.setAttribute("name","radioButton");
radioButton.setAttribute("value","checked");
}
八、Struts+AJAX實例:
1、簡單的返回文本
Struts端:(頁面JS端讀取responseText)
/**
* 檢測VIP客戶網(wǎng)址
* (-2:處理出錯,-1:沒有數(shù)據(jù),0:有客戶數(shù)據(jù)且不是VIP,1:已經(jīng)是VIP)
*/
public void checkVipuser(ActionMapping actionMapping,ActionForm actionForm,HttpServletRequest request,
HttpServletResponse response) {
PrintWriter out=null;
int result=-2;
try{
out = response.getWriter();
String weburl=request.getParameter("weburl");
//后臺處理(省略)
out.print(result);
out.flush();
out.close();
}catch(Exception e){
e.printStackTrace();
}
}
2、返回XML* 檢測VIP客戶網(wǎng)址
* (-2:處理出錯,-1:沒有數(shù)據(jù),0:有客戶數(shù)據(jù)且不是VIP,1:已經(jīng)是VIP)
*/
public void checkVipuser(ActionMapping actionMapping,ActionForm actionForm,HttpServletRequest request,
HttpServletResponse response) {
PrintWriter out=null;
int result=-2;
try{
out = response.getWriter();
String weburl=request.getParameter("weburl");
//后臺處理(省略)
out.print(result);
out.flush();
out.close();
}catch(Exception e){
e.printStackTrace();
}
}
Struts端:(頁面JS端讀取responseXML)
public void searchEmployee(ActionMappingactionMapping,ActionFormactionForm,HttpServletRequestrequest,HttpServletResponseresponse){
response.setContentType("text/xml;charset=utf-8");
PrintWriterout=null;
try{
out=response.getWriter();
out.print("<?xmlversion=\"1.0\"encoding=\"utf-8\"?>");
out.print("<rootnode>");
out.print("<pro1='valu1' pro2='valu2' pro3='valu3' pro4='valu4'/>");
out.print("</rootnode>");
out.flush();
out.close();
}catch(Exceptione){
e.printStackTrace();
out.print("<?xmlversion=\"1.0\"encoding=\"utf-8\"?>");
out.flush();
out.close();
}
}
response.setContentType("text/xml;charset=utf-8");
PrintWriterout=null;
try{
out=response.getWriter();
out.print("<?xmlversion=\"1.0\"encoding=\"utf-8\"?>");
out.print("<rootnode>");
out.print("<pro1='valu1' pro2='valu2' pro3='valu3' pro4='valu4'/>");
out.print("</rootnode>");
out.flush();
out.close();
}catch(Exceptione){
e.printStackTrace();
out.print("<?xmlversion=\"1.0\"encoding=\"utf-8\"?>");
out.flush();
out.close();
}
}