关于Ajax的问题

来源:百度知道 编辑:UC知道 时间:2024/05/22 15:27:30
初学Ajax,做了个小例子,但是当点击按钮是没有响应,求高人指点。

innerHTMlTest.html

<html>
<head>
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
else if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
}

function startRequest(){
createXMLHttpRequest();
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.open("GET","innerHTML.xml",true);
xmlHttp.send(null);
}

function handleStateChange(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
document.getElementById("results").innerHTML = xmlHttp.responseText;
}
}
}
</script>
</head>
<body>
<form action="">
<input type="button" value="搜索"

改为这样测试:
function handleStateChange(){
if(xmlHttp.readyState==4){
alert(xmlHttp.status)
if(xmlHttp.status==200){

document.getElementById("results").innerHTML = xmlHttp.responseText;
}
}
}
经查"alert(xmlHttp.status)"弹出404而非200,所以没反应了.也即是找不到文件路径.

xml文件可如下装载:
var source;
var sourceName = "innerHTML.xml";
var source = new ActiveXObject('Microsoft.XMLDOM'); //创建一个 MSXML解析器实例
source.async = false;
source.load(sourceName); //装入XML文档

具体如何可再研究一下.