我仔细检查了我的代码,我找不到哪个部分我做错了。每次我点击按钮,它都不会从我的generate.php
中检索文件的index.php
<html>
<head>
<title>Title</title>
<script type="text/javascript">
function myLoad(){
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}else{
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
document.getElementById('par').innerHTML == xmlhttp.responseText;
}
}
xmlhttp.open('GET', 'generate.php', true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="par"></div>
<input type="button" value="Click" onclick="myLoad();">
</body>
</html>
GENERATE.PHP
<?php
echo 'Hello';
?>
答案 0 :(得分:7)
document.getElementById('par').innerHTML == xmlhttp.responseText;
^ // here is problem it should be =
document.getElementById('par').innerHTML = xmlhttp.responseText;
答案 1 :(得分:1)
document.getElementById('par').innerHTML == xmlhttp.responseText;
------------------------------------------^ // Make it just =
答案 2 :(得分:0)
将此代码复制到index.php文件中。我相信它会解决你的问题,当你点击按钮时,它会从generate.php文件调用'Hello'。
<html>
<head>
<title>Title</title>
<script type="text/javascript">
function myLoad()
{
var xmlhttp;
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}
else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("par").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","generate.php",true);
xmlhttp.send();
}
</script>
</head>
<body>
<input type="button" value="Click" onclick="myLoad();">
<div id="par"></div>
</body>
</html>