<script language="Javascript">
var XMLHttpReq = false;
// 創建 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){}
}
}
}
//發送請求函數(提交XML格式的請求參數)
function sendRequest(url,XML){
createXMLHttpRequest();
XMLHttpReq.open("POST",url,true);
XMLHttpReq.onreadystatechange = processResponse;//指定響應函數
XMLHttpReq.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
XMLHttpReq.send(XML);//發送請求
}
//創建XML
function createXML(){
var XML = "<user>";
xml = XML + "<name>sunjianbin<\/name>";
xml = XML + "<password>7980421<\/passWord>";
xml = XML + "<\/user>";
return XML;
}
//處理返回信息函數(處理XML格式的返回信息)
function processResponse(){
if(XMLHttpReq.readyState == 4){ // 判斷對象狀態
if(XMLHttpReq.status == 200){ // 信息已經成功返回,開始處理信息
readXML();
}else { //頁面不正常
window.alert("您所請求的頁面有異常。");
}
}
}
function readXML(){
var objects=XMLHttpReq.responseXML.getElementsByTagName("user");
for(var i=0;i<objects.length;i++){
var object = objects[i];
var name=object.getElementsByTagName("name")[0].firstChild.data;
var password=object.getElementsByTagName("passWord")[0].firstChild.data;
alert("name>>>"+name+",password>>>"+passWord);
}
}
function sendto(){
var url="/test/servlet/test";
var xml=createXML();
sendRequest(url,XML)
}
</script>