XMLHttpRequest.open无效的上下文错误

时间:2017-06-04 08:30:16

标签: javascript php ajax

我正在

  

InvalidStateError:XMLHttpRequest的上下文无效。

在以下xmlhttp.open开头的行上:

function ShowData(str) {
    document.write('<div>Started</div>');
    if (str == "") {
        document.write('str null');
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else {
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            document.write('<div>Setting http</div>');
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            document.write('<div>onreadystatechange event</div>');
            if (this.readyState == 4 && this.status == 200) {
                document.write('<div>Getting response</div>');
                document.getElementById("txtHint").innerHTML = this.responseText;
            }
        };
        xmlhttp.open("GET","testquery.php", true); // error here
        xmlhttp.send();
    }
}

在这里&#34; testquery.php&#34;,我已经通过在没有JS的情况下回显html中的结果来测试它,并且数据被返回OK:

<?php

$con=mysqli_connect("xxx","xxx","xxx") or die ("failed to connect to server !!");
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"testdb");
$sql="SELECT * FROM tblComments";
$result = mysqli_query($con,$sql);

echo "<table>";
while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['comment'] . "</td>";
    echo "</tr>";
}
echo "</table>";
mysqli_close($con);

?>

请保持友好的友好!感谢。

0 个答案:

没有答案