I'm new to PHP, and i'm trying to fetch data from PHP to JavaScript. When i do that, JavaScript always giving me the same error "Unexpected token < in JSON at position 0". I don't have any idea what to do. I'm stuck...Help Help ! x)
There is the code
JavaScript
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
var myObj = JSON.parse(this.responseText);
document.getElementById("demo").innerHTML = myObj.name;
}
};
xmlhttp.open("GET", "data.php", true);
xmlhttp.send();
PHP
<?php
$myObj->name = "John";
$myObj->age = 30;
$myObj->city = "New York";
$myJSON = json_encode($myObj);
echo $myJSON;
?>
<
which if course is the first character of any HTML tag. Use your browser's network inspector to view the raw response coming back from the php script and check what the issue is. You need to ensure it always returns JSON alone, no other content or characters