I've got this AJAX function:
function fetchSocialCount(type,fileSrc){
var count = null;
var req = new XMLHttpRequest();
req.onload = function(){
if(req.status === 200 && req.readyState === 4){
console.log(req.responseText);
count = JSON.parse(req.responseText);
}
}
req.open("GET","../scripts/php/returnSocialCount.php?type=" + type + "&fileSrc=" + fileSrc,false);
req.send();
if(count !== null){
return count;
}
else{
console.log("The AJAX request has failed.");
}
}
But when I run it, I get a 'SyntaxError: Unexpected end of input'.
error. The Chrome Dev Tools also covers the count = JSON.parse(req.responseText);
part in blue, so I'm guessing it's something wrong with the JSON I'm recieving. From the PHP script, I'm sending a pretty complex JSON object, so I tried to send a really basic one and it worked without problems.
This is the part of the PHP script that sends the response:
echo '{"likes":'.$json->videoListArray[$i]->likes . ',"dislikes":' . $json->videoListArray[$i]->dislikes . '}';
Is there something wrong with the syntax of the echo
? What's the problem?
Thanks.