0

这是我关于 PHP 的第一个问题,所以请多多包涵。我一直在关注 phpacademy.org 的教程。

我被困在一个介绍 AJAX 的教程上。我以导师的身份输入了确切的代码,但它仍然无法正常工作。

我搜索了很多,但它没有一点帮助。有人可以帮我吗?这是我的代码:

<html>
<head>
<script type="text/javascript">
function load(){

if(window.XMLHttpRequest)
    xmlhttp=new XMLHttpRequest();
else
    xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');

xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4 && xmlhttp.status==200){
    document.getElementsById('adiv').innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open('GET', 'AJAX.inc.php', true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="adiv"></div>
<input type="submit" value="Submit" onclick="load();">
</body>
</html>

这是 AJAX.inc.php 文件:

<?php
echo 'Hello AJAX';
?>

然而,来自 w3school.com 的另一个示例正在运行。

可能重复: AJAX 不能与 XAMPP 一起使用,还是不可能

但是这个问题没有得到正确的回答(或者我不明白)。有人请澄清一下吗?

4

2 回答 2

2

没有document.getElementsById方法。

document.getElementsById('adiv').innerHTML=xmlhttp.responseText;

应该

document.getElementById('adiv').innerHTML=xmlhttp.responseText;
于 2012-08-14T01:57:32.930 回答
1
<html>
<head>
<script type="text/javascript">
function load(){

if(window.XMLHttpRequest)
    {
    xmlhttp=new XMLHttpRequest();
    }
else
    {
    xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
    }
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4 && xmlhttp.status==200){
    document.getElementById('adiv').innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open('GET', 'ajax_php.php', true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="adiv"></div>
<input type="submit" value="Submit" onclick="load();">
</body>
</html>
于 2013-09-23T11:33:21.537 回答