52,797
社区成员
发帖
与我相关
我的任务
分享
方法一:
xmlHttp.open("GET", "default.aspx?param="+Math.random(), true);
或者
xmlHttp.open("GET", "default.aspx?param="+new Date().getTime(), true);
方法二:
xmlHttp.open("GET", "default.aspx", true);
xmlHttp.setRequestHeader("If-Modified-Since","0");
xmlHttp.send(null);
<html>
<head>
<script>
var xmlHttp;
function createXMLHttpRequest()
{
if(window.ActiveXObject)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
}
function startRequest(province)
{
createXMLHttpRequest();
try
{
xmlHttp.onreadystatechange = function(){handleStateChange(province);}
xmlHttp.open("GET", "data.xml", true);
xmlHttp.send(null);
}
catch(exception)
{
alert("xmlHttp Fail");
}
}
function handleStateChange(province)
{
if(xmlHttp.readyState == 4)
{
if (xmlHttp.status == 200 || xmlHttp.status == 0)
{
var root= xmlHttp.responseXML.documentElement;
try
{
var citySelect=document.getElementById("city");
citySelect.options.length=0;
var province=root.getElementsByTagName(province)[0];
var citys=province.getElementsByTagName("city");
for(var i=0; i<citys.length; i++)
{
var city=citys[i];
var cityName=city.getElementsByTagName("name")[0].firstChild.nodeValue;
var cityValue=city.getElementsByTagName("id")[0].firstChild.nodeValue;
var option=new Option(cityName, cityValue);
citySelect.options.add(option);
}
}
catch(exception)
{
alert("The node is not exist");
}
}
}
}
function DisplayCity(province)
{
if(province == "")
{
var citySelect=document.getElementById("city");
citySelect.options.length=0;
return;
}
else
{
startRequest(province);
}
}
</script>
</head>
<body onload="startRequest('Shanxi');">
<select id= "province" onchange="DisplayCity(this.options[this.selectedIndex].text);" align="center" style="width:100px">
<option value="0"></option>
<option value="1" selected="selected">Shanxi</option>
<option value="2">Guangdong</option>
</select>
<br>
<select id= "city" align="center" style="width:100px">
</select>
</body>
</html>
<?xml version="1.0" encoding="GB2312"?>
<root>
<Shanxi>
<city>
<name>Xian</name>
<id>1</id>
</city>
<city>
<name>Xianyang</name>
<id>2</id>
</city>
<city>
<name>Baoji</name>
<id>3</id>
</city>
<city>
<name>Weinan</name>
<id>4</id>
</city>
<city>
<name>Hanzhong</name>
<id>5</id>
</city>
</Shanxi>
<Guangdong>
<city>
<name>Shenzhen</name>
<id>1</id>
</city>
<city>
<name>Dongguan</name>
<id>2</id>
</city>
<city>
<name>Zhongshan</name>
<id>3</id>
</city>
<city>
<name>Huizhou</name>
<id>4</id>
</city>
<city>
<name>Foshan</name>
<id>5</id>
</city>
</Guangdong>
</root>
function initAjax()
{
var ajax = false;
try {
ajax = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
ajax = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
ajax = false;
}
}
if (! ajax && typeof XMLHttpRequest != 'undefined') {
ajax = new XMLHttpRequest();
}
return ajax;
}
function getAjax(httpurl, requests, div)
{
if (typeof(httpurl, requests, div) == 'undefined')
{
return false;
}
var url = httpurl + requests;
var show = document.getElementById(div);
var ajax = initAjax();
ajax.open("GET", url, true);
ajax.onreadystatechange = function() {
if (ajax.readyState == 4 && ajax.status == 200) {
show.innerHTML = ajax.responseText;
}
}
ajax.send(null);
}