0

我仔细检查了我的代码,但找不到我做错的部分。每次,我单击按钮,它都不会从我的 generate.php 中检索文件

索引.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>

生成.PHP

<?php
    echo 'Hello';
?>
4

3 回答 3

7
document.getElementById('par').innerHTML == xmlhttp.responseText;
                                         ^ // here is problem it should be =

document.getElementById('par').innerHTML = xmlhttp.responseText;
于 2013-01-24T06:44:33.387 回答
1

错字在这里:

document.getElementById('par').innerHTML == xmlhttp.responseText;
------------------------------------------^ // Make it just =
于 2013-01-24T06:45:16.080 回答
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>
于 2013-01-24T07:02:47.950 回答