I don't understand why my json.parse()
isn't working. The error is probably right before my eyes but I can't find it. Here is the code that I have:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Stage galerij</title>
<link rel="icon" href="img/website/photo.jpg" type="image/jpg" sizes="any">
<link rel="stylesheet" href="css/main.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
</head>
<body>
<div id="id01"></div>
<script>
var xmlhttp = new XMLHttpRequest();
var url = "http://www.clouddesk.be/galerij/includes/galerij_array.php";
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
myFunction(xmlhttp.responseText);
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
function myFunction(response) {
var arr = JSON.parse(response);
var i;
var out = "<table>";
for (i = 0; i < arr.length; i++) {
out += "<tr><td>" +
arr[i].id +
"</td><td>" +
arr[i].File_path +
"</td><td>" +
arr[i].uitleg +
"</td></tr>";
}
out += "</table>";
document.getElementById("id01").innerHTML = out;
}
</script>
</body>
</html>
And here is the code of the php file:
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
$conn = new mysqli("localhost", "clouddesk_robbe", "password", "clouddesk_stage");
$result = $conn->query("SELECT * FROM Fotos");
$outp = "[";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
if ($outp != "[") {$outp .= ",";}
$outp .= '{"id":"' . $rs["idFotos"] . '",';
$outp .= '"File_path":"' . $rs["Foto_File"] . '",';
$outp .= '"uitleg":"' . $rs["Uitleg"] . '",';
$outp .= '"alt":"'. $rs["Alternatief"] . '"}';
}
$outp .="]";
$conn->close();
echo($outp);
?>
It outputs this json string:
[{"id":"2","File_path":"imggallerijAfbeelding-61.png","uitleg":"Zebra verliest zijn strepen.","alt":"Stressige zebra"},{"id":"3","File_path":"imggallerijdownload(1).jpg","uitleg":"Tomatensoep met balletjes: Nog een keer blazen en ik sla op je bek.","alt":"Grappige cartoon"},{"id":"4","File_path":"imggallerijdownload.jpg","uitleg":"Brilsmurf met een raar gezicht","alt":"Brilsmurf"},{"id":"5","File_path":"imggallerijdownload(2).jpg","uitleg":"Mooi avondlijk woestijnuitzicht.","alt":"Woestijnuitzicht"},{"id":"8","File_path":"imggallerijimage1.jpg","uitleg":"Een mooi blaadje dat door het herfstseizoen van de boom is gevallen en eenzaam op de grond licht. Het is getrokken in de late herfstzon.","alt":"Herfstblaadje op de grond"},{"id":"9","File_path":"imggallerij7.jpg","uitleg":"Een kleurrijk paneel dat elke mens wel terug wat opfleurd.","alt":"Een kleurig paneel"},{"id":"10","File_path":"imggallerijimages(1).jpg","uitleg":"Mooie foto van een golf die in de late avond getrokken is. Je ziet de reflectie van de zo in de buiging van de golf.","alt":"Golf in de avondzon"},{"id":"12","File_path":"imggallerijimages.jpg","uitleg":"Mooie auto, het is een dure rode lexus getrokken langs de linker voorkant.","alt":"Mooie rode lexus"},{"id":"13","File_path":"imggallerijNikon-D810-Image-Sample-6.jpg","uitleg":"Een uitzicht op een onbewoond eiland zoals je je voorstelt in je verbeelding","alt":"Een onbewoond eiland"},{"id":"14","File_path":"imggallerijimage-slider-2.jpg","uitleg":"Het huis van de film up in de lucht doormiddel van duizenden ballonen aan de schoorsteen.","alt":"Huis in de lucht"},{"id":"15","File_path":"imggallerijdemo-image.jpg","uitleg":"Een mooie foto van een zwaan.","alt":"foto van een zwaan"}]
I copied practically everything from this page of w3schools.com:
http://www.w3schools.com/json/json_example.asp
When I tested it with the original code of w3schools.com it worked perfectly. But when I changed the URL variable and the array names, it doesn't work anymore. I also tested if the json ouput was valid with an online validator and it said it was completely fine.
See Question&Answers more detail:
os