@ page language="java"import="java.util.*"pageEncoding="UTF-8"%>
Ajax例子二TITLE>varxmlHttp= null;functioncreateXMLHttpRequest() {if(xmlHttp== null){if(window.ActiveXObject) {
xmlHttp= newActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest) {
xmlHttp= newXMLHttpRequest();
}else{
alert("无法创建 XML HTTP 对象");
}
}
}functiongetInfo() {//后台提交
varurl= "sample2Action.jsp";varqueryStr= "param1=hello¶m2=大家好";
createXMLHttpRequest();
xmlHttp.onreadystatechange=handleStatsChange;
xmlHttp.open("post", url,true);//此代码需要放置在open方法下面,否则异常,后台获取不到查询参数
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(queryStr);
}functionhandleStatsChange() {vardivInfo=document.getElementById("divInfo");if(xmlHttp.readyState== 4) {if(xmlHttp.status== 200) {varaddDocument=xmlHttp.responseXML;vardocs=addDocument.getElementsByTagName("doc");//创建table对象,然后将xml解析结果放置进去
vartableNode=document.createElement("table");
tableNode.setAttribute("border","1");//设置标题栏
vartr=tableNode.insertRow(0);vartd=tr.insertCell();
td.appendChild(document.createTextNode("title"));
td=tr.insertCell();
td.appendChild(document.createTextNode("content"));//下面是内容
for(vari=0; i
tr=tableNode.insertRow(i+1);for(varj=0; j<2; j++) {
td=tr.insertCell(j);vartitle1=doc.childNodes[j];vartext=document.createTextNode(title1.firstChild.nodeValue);
td.appendChild(text);
}
}
divInfo.appendChild(tableNode);
}
}
}SCRIPT>
HEAD>
DIV>
BODY>
HTML>