W3School의 수업을 이해하는 데 열심히 노력했지만 어떻게 든 작동하지 않습니다!알 수없는 코드 오류
여러분이 훨씬 잘 이해할 수 있도록 편집했습니다.
이
는 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) {
\t \t myObj = JSON.stringify(this.response);
myObj1 = JSON.parse(myObj);
\t \t document.getElementById("demo").innerHTML = myObj1.stuid;
\t \t alert(myObj1);
\t \t
}
};
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);
?>
와에게 다시입니다 sult of 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
답만을 반환합니다. 여기서 무엇이 잘못 되었습니까?
나는 무엇이 잘못되었는지를 모른다. 누군가 실수를 지적 할 수 있습니까?
'노래에서 마크 업을 제거하십시오.php' – Andreas
나는 [w3fools] (http://www.w3fools.com/)의 팬이 아니다. – Nope
아, 그걸 잊어 버렸지 만, 그게 그냥 정의되지 않았다. – Arkonsol