
[英]Error: Open (GetCommState) : Unknown error code 1 - Nodejs, express, SerialPort
[英]Unknown Code Error
我一直在尽最大的努力来理解W3Schools中的课程,但是它不起作用!
我已经对其进行了编辑,以使你们得到更好的结果。
这是index.html页面:
<!DOCTYPE html> <html> <body> <h2>Get data as JSON from a PHP file on the server.</h2> <p id="demo"></p> <script> var myObj, i, x, j = ""; var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { myObj = JSON.stringify(this.response); myObj1 = JSON.parse(myObj); document.getElementById("demo").innerHTML = myObj1.stuid; alert(myObj1); } }; xmlhttp.open("GET", "sing.php", true); xmlhttp.send(); </script> </body> </html>
这是sing.php
<?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "sacapp"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $result = $conn->query("SELECT stuid, stuname, stucourse, stustat, stulyear, stulog FROM stuinfo"); $outp = $result->fetch_all(MYSQLI_ASSOC); echo json_encode($outp); ?>
这是myObj1
[{"stuid":"10-00002","stuname":"Meratis, Velino","stucourse":"InfoTech","stustat":"0","stulyear":"4","stulog":"feb 16 2017"},{"stuid":"10-00003","stuname":"Melker, Alana","stucourse":"CivilEngi","stustat":"1","stulyear":"5","stulog":"feb 16 2017"}]
myObj1
[{"stuid":"10-00002","stuname":"Meratis, Velino","stucourse":"InfoTech","stustat":"0","stulyear":"4","stulog":"feb 16 2017"},{"stuid":"10-00003","stuname":"Melker, Alana","stucourse":"CivilEngi","stustat":"1","stulyear":"5","stulog":"feb 16 2017"}]
但是document.getElementById("demo").innerHTML = myObj1.stuid;
只返回Undefined
答案...这里出了什么问题?
我不知道这到底是怎么了。 有人可以指出任何错误吗?
从sing.php中删除html,仅返回JSON。 即使您在浏览器中看到了正确的输出,但是如果您查看源代码,也会注意到,等标记。 您的脚本正在查看这些内容并失败了。
看起来AJAX请求返回了HTML,您可以使用警报检查返回的内容,将代码更改为此:
if (this.readyState == 4 && this.status == 200) {
alert(xmlhttp.responseText);
}
如果警报中有HTML,则应在以下行中检查sing.php页面的路径是否正确:
xmlhttp.open("GET", "sing.php", true);
更新的答案。
好的,我检查了所有内容并进行了本地设置。 您的代码中有很多错误。
让我们看一下index.html。
它应该看起来像这样。 (我在您出错的地方添加了注释。)
<!DOCTYPE html>
<html>
<body>
<h2>Get data as JSON from a PHP file on the server.</h2>
<p id="demo"></p>
<script>
var myObj, i, x, j = "";
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
console.log(this);
if (this.readyState == 4 && this.status == 200) {
myObj = JSON.parse(this.response); //**IT'S 'response' NOT 'responseText' To look at the object data, you can do console.log(myObj) and see what the object looks like. in the developer window**
document.getElementById("demo").innerHTML = myObj.student_id; //**YOU DON'T HAVE A PROPERTY 'id' BUT YOU DO HAVE 'student_id', again look at the json object in the developer window. **
}
};
xmlhttp.open("GET", "sing.php", true);
xmlhttp.send();
</script>
</body>
</html>
至于“ sing.php”,因为我没有您的数据库,所以我创建了自己的数组,以便可以合并它们,因为我认为这就是您要尝试的方法。
<?php
$outp = array(
'student_id' => '10-00000',
'student_name' => 'Breaker M. North',
'student_course' => 'BSIT',
'student_stat' => 0,
'student_log' => 1
);
$outp2 = array(
"stu_log" => 2,
"course_name" => "IT 202",
"student_id" => "10-00000"
);
echo json_encode(array_merge($outp, $outp2));
?>
当您执行echo json_encode($outp + $outp2);
您实际上是在创建新数组,并将$outp
和$outp2
作为单独的值。 这就是为什么要使用[]的原因,您有两个值,而不仅仅是一个。 然后,您将无法undefined
因为您无法访问属性,而是访问json数组的第一个元素。
附带说明,您可以通过JOIN on student_id
使用JOIN on student_id
仅创建一个SQL请求...但这不是问题的一部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.