In my javascript I'm getting this error:
Uncaught SyntaxError: Unexpected token P in JSON at position 0, at JSON.parse (), at XMLHttpRequest.req.onreadystatechange
when trying to receive some stuff from a PHP script. I've seen some similar questions but I couldn't find a solution for my case.
My server PHP code:
<?php
header('Content-Type: application/json');
$myObj->name = "John";
$myObj->age = 30;
$myObj->city = "New York";
$myJSON = json_encode($myObj,JSON_UNESCAPED_UNICODE);
echo $myJSON;
?>
My client javascript:
var req = new XMLHttpRequest();
req.onreadystatechange = function () {
if (this.readyState == XMLHttpRequest.DONE && this.status == 200) {
var s = req.responseText;
var users = JSON.parse(s);
console.table(s);
}
}
req.open("GET", "./get_info.php", true);
When I run the PHP file using the browser I get this:
{"name":"John","age":30,"city":"New York"}
which I believe is correct.
Any suggestion?
$myObj = new stdClass;
at least