AJAX的经典调用
要求:---- ajax不能在一个页面加载 或者后面需要关闭页面的js代码段中使用.
//创建XML对象
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} else {
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}
}
//处理函数
function callback()
{
if(xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
parseMessage();
}else{
alert("Not able to retrieve description"+xmlHttp.statusText);
}
}
}
function parseMessage()
{
var xmlDoc=xmlHttp.responseXML.documentElement;
var xSel=xmlDoc.getElementsByTagName('hours');
var select_root=document.getElementById('timeControl.hour');
select_root.options.length=0;
for(var i=0;i<xSel.length;i++)
{
var xValue=xSel[i].childNodes[0].firstChild.nodeValue;
var xText=xSel[i].childNodes[1].firstChild.nodeValue;
var option=new Option(xText,xValue);//Option是包含value和text的对象
try{
select_root.add(option);
}catch(e){
}
}
}
//调用服务器
function callServer1(){
createXMLHttpRequest();
// var name=document.getElementById('name').value;
var url="<%=request.getContextPath()%>/adv/AdvSpaceManageAction.do?act=getDate"+"&time="+new Date();
alert(url);
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = callback;
xmlHttp.send(null);
}
要求:---- ajax不能在一个页面加载 或者后面需要关闭页面的js代码段中使用.