Javascript XMLHttpRequest.open无效上下文错误
我要走了 InvalidStateError:XMLHttpRequest的上下文无效 在以Javascript XMLHttpRequest.open无效上下文错误,javascript,php,ajax,Javascript,Php,Ajax,我要走了 InvalidStateError:XMLHttpRequest的上下文无效 在以xmlhttp.open开头的行上,打开以下内容: function ShowData(str) { document.write('<div>Started</div>'); if (str == "") { document.write('str null'); document.getElementById("txtHint")
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();
}
}
函数ShowData(str){
document.write('start');
如果(str==“”){
document.write('strnull');
document.getElementById(“txtHint”).innerHTML=“”;
返回;
}否则{
if(window.XMLHttpRequest){
//IE7+、Firefox、Chrome、Opera、Safari的代码
document.write('settinghttp');
xmlhttp=新的XMLHttpRequest();
}否则{
//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数(){
document.write('onreadystatechange事件');
if(this.readyState==4&&this.status==200){
document.write(‘获取响应’);
document.getElementById(“txtHint”).innerHTML=this.responseText;
}
};
open(“GET”,“testquery.php”,true);//此处出错
xmlhttp.send();
}
}
这里是“testquery.php”,我通过在不使用JS的情况下在html中回显结果对其进行了测试,数据返回正常:
<?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);
?>
请保持友好!谢谢。什么是浏览器?您是否从其他浏览器进行了测试?Firefox,我刚刚在Chrome和Edge中进行了测试,得到了相同的结果我无法重现问题。什么是浏览器?您是否从其他浏览器进行了测试?Firefox,我刚刚在Chrome和Edge中进行了测试,得到了相同的结果我无法重现问题。