I am passing sql table rows to a javascript file to be shown once the website page has loaded:
php code:
$sql = "SELECT * FROM `publication_information`";
$pubs = $conn->query($sql);
while ($row = mysqli_fetch_assoc($pubs)) {
$existing_publications[] = $row;
}
$willies = json_encode($existing_publications);
echo $willies;
javascript code:
function WORK(){
var xhr = new XMLHttpRequest();
var url = "/shoba/publications.php";
xhr.onreadystatechange = function() {
// Check if the request is completed and successful
if (xhr.readyState == 4 && xhr.status == 200) {
// Parse the JSON response
var data = JSON.parse(xhr.responseText);
// Do something with the data
console.log(data+" success");
} else {
console.log(error);
}
};
xhr.open("GET", url, true);
xhr.send();
alert("reached function end");
}
on calling the function, the alert is reached but i get the uncaught error:
VM65:1 Uncaught SyntaxError: Unexpected end of JSON input
But I cannot spot what is wrong, to my knowledge , since I used an auto function it should work, but it sadly does not. Can you help tell me what's wrong?