才看ajax不久,遇到这个问题,一直报错 click事件提示 缺少对象,怎么也没看出代码问题,给点提示吧! <script type="text/javascript">
var xmlhttp;
function loadXMLDoc() {
xmlhttp = null;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest(); //Firefox,Opera,IE7,
}
else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp != null) {
xmlhttp.onreadystatechange = state_Change();
xmlhttp.open("get", "/test1.txt", true);
xmlhttp.send();
}
else {
alert("浏览器不支持XMLHTTP!");
}
} function state_Change(){
if (xmlhttp.readyState==4) {//4loaded
if (xmlhttp.status==200) {//OK
document.getElementById('T1').innerHTML=xmlhttp.responseText;
alert(xmlhttp.responsetxt);
}
else{
alert("Problem retrieving data:"+xmlhttp.statusText);
}
}
}
</script>
</head>
<body>
<div id="T1" style="border:1px solid black;height:40px;width:100px;padding:5">
</div>
<br />
<input type="button" value="Click" onclick="loadXMLDoc()" />
</body>
var xmlhttp;
function loadXMLDoc() {
xmlhttp = null;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest(); //Firefox,Opera,IE7,
}
else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp != null) {
xmlhttp.onreadystatechange = state_Change();
xmlhttp.open("get", "/test1.txt", true);
xmlhttp.send();
}
else {
alert("浏览器不支持XMLHTTP!");
}
} function state_Change(){
if (xmlhttp.readyState==4) {//4loaded
if (xmlhttp.status==200) {//OK
document.getElementById('T1').innerHTML=xmlhttp.responseText;
alert(xmlhttp.responsetxt);
}
else{
alert("Problem retrieving data:"+xmlhttp.statusText);
}
}
}
</script>
</head>
<body>
<div id="T1" style="border:1px solid black;height:40px;width:100px;padding:5">
</div>
<br />
<input type="button" value="Click" onclick="loadXMLDoc()" />
</body>
改成
xmlhttp.onreadystatechange = state_Change;
改为xmlhttp.open("get", "/test1.txt", false);
在火狐下是可以获取文本值的
能否解释一下,这里引用方法为什么不加括号.其实应该是
xmlhttp.onreadystatechange = function() {
...
}这里应该是定义一个function,而不是调用一个function