Hey, I'm in desperate need. I have made a JSON object with PHP named userAuth and I am trying to pass it to a JS file with Ajax, here is my PHP code (auth.php):
$userAuth->access = 1;
json_encode($userAuth);
Here is my 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();
Yet I am being thrown this error in console:
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
I have no idea why it's not working, I've followed a tutorial and I'm desperate!
Thanks in advance & Regards,
flugs
1 Answer(s)