I know this is a JS section, but I’m desperate.
I have JSON objects made in PHP and I want to pass them into JS with AJAX. I have followed a tutorial and I don’t know where I’ve gone wrong! Here is my code:
PHP (Auth.php):
$userAuth->access = 1;
$userAuth->uid = $uid;
$userAuthArray = json_encode($userAuth);
echo $userAuthArray;
JS (Auth.js):
const xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
const userAuth = JSON.parse(this.responseText);
console.log(userAuth.access);
}
};
xmlhttp.open("GET", "auth.php", true);
xmlhttp.send();
The error I am getting is this:
VM38:1 Uncaught SyntaxError: Unexpected token A in JSON at position 0
at JSON.parse (<anonymous>)
at XMLHttpRequest.xmlhttp.onreadystatechange (auth.js:4)
xmlhttp.onreadystatechange @ auth.js:4
XMLHttpRequest.send (async)
(anonymous) @ auth.js:10
Thanks in advance, and apologies!
flugs