Ajax 处女秀
将Ajax in Action 第9章的Server端,改成了SERVLET形式的
代码如下:
<html>
<head>
<META http-equiv=Content-Type content= "text/html;charset=UTF-8 ">
</head>
<script language= "javascript ">
var XMLHttpReq;
function createXMLHttpRequest(){
if(window.XMLHttpRequest){
XMLHttpReq = new XMLHttpRequest();
}else if(window.ActiveXObject){
try{
XMLHttpReq = new ActiveXObject( "Msxml2.XMLHTTP ");
}catch(E){
try{
XMLHttpReq = new ActiveXObject( "Microsoft.XMLHTTP ");
}catch(E){}
}
}
}
//¡¤¡罿?¦ʿ¡¥?
function sendRequest(oElem,oTarget){
var strValue = oElem.options[oElem.selectedIndex].value;
var URL = "simpleservlet ";
var strParams = 'q= ' + strValue +
"&f= " + oTarget.form.name +
"&e= " + oTarget.name;
createXMLHttpRequest();
XMLHttpReq.onreadystatechange=processResponse;
XMLHttpReq.open( "POST ",URL,true);
//POST方式,要设置Header
var contentType= 'application/x-www-form-urlencoded ';
XMLHttpReq.setRequestHeader( 'Content-Type ', contentType);
XMLHttpReq.send(strParams);
}
//¡㻭§²?o¡¥?
function processResponse(){
if(XMLHttpReq.readyState==4){
if(XMLHttpReq.status==200){
//readXml();
read2Xml();
}else{
window.alert( "Error! "+XMLHttpReq.status);
}
}
}
function read2Xml(){
var xmlDoc = XMLHttpReq.responseXML.documentElement;
if(null == xmlDoc)
window.alert( "xmlDoc is null! ");
var xSel = xmlDoc.
getElementsByTagName( 'selectElement ')[0];
var strFName = xSel.
childNodes[0].firstChild.nodeValue;
var strEName = xSel.
childNodes[1].firstChild.nodeValue;
&nbs