dongpao2871 2017-06-04 08:30
浏览 406

XMLHttpRequest.open无效的上下文错误

I'm getting

InvalidStateError: XMLHttpRequest has an invalid context.

on the line beginning with xmlhttp.open below:

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();
    }
}

Here's "testquery.php", I've tested it by just echoing the results in the html without JS and the data is returned 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);

?>

Please keep it noob friendly! Thanks.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 office打开卡退(新电脑重装office系统后)
    • ¥300 FLUENT 火箭发动机燃烧EDC仿真
    • ¥15 【Hadoop 问题】Hadoop编译所遇问题hadoop-common: make failed with error code 2
    • ¥15 vb6.0+webbrowser无法加载某个网页求解
    • ¥15 RPA财务机器人采购付款流程
    • ¥15 计算机图形多边形及三次样条曲线绘制
    • ¥15 根据protues画的图用keil写程序
    • ¥200 如何使用postGis实现最短领规划?
    • ¥15 pyinstaller打包错误
    • ¥20 cesm的气溶胶排放文件