0

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.

8
  • 2
    You can use the browser developer tools to see what the actual response text looks like. Commented Oct 8, 2015 at 17:14
  • @Pointy Thank you. Do you know where might that option be in Chrome? Commented Oct 8, 2015 at 17:15
  • press F12 then choose the console tab. then try doing an ajax request again it will appear there. Commented Oct 8, 2015 at 17:17
  • 5
    I wouldn't advise creating the JSON like that. Create an associative array in normal PHP syntax and then use echo json_encode($array); to create the JSON and return it to the ajax call. You run the risk of introducing a syntax error by creating this manually... Commented Oct 8, 2015 at 17:17
  • try : echo '{"likes":"'.$json->videoListArray[$i]->likes . '","dislikes":"' . $json->videoListArray[$i]->dislikes . '"}'; Commented Oct 8, 2015 at 17:18

2 Answers 2

3

You need to use json_encode (PHP json_encode manual page) and echo it like so:

echo json_encode(array('likes'=>$json->videoListArray[$i]->likes, 'dislikes'=>$json->videoListArray[$i]->dislikes));

My guess is you have a comma, semi-colon, or some other character in your response that is causing the issue. json_encode will make sure everything is formatted nicely.

EDIT - War10ck beat me to the punch.

Sign up to request clarification or add additional context in comments.

Comments

1

It appears that there is probably a syntax error in your JSON that you're returning. It's not usually a good idea to manually create JSON for this very reason. Try creating an associative array and then use json_encode() to return the desired data to the ajax call:

// Create the array
$data = array();

// Add the data
$data['likes'] = $json->videoListArray[$i]->likes;
$data['dislikes'] = $json->videoListArray[$i]->dislikes;

// Generate JSON from the array and return the desired results
echo json_encode($data);

Reference Documentation:

2 Comments

Thank you. I didn't know associative arrays encode to JSON objects.
@aCodingN00b You're welcome. Any array structure in php usually encodes to JSON pretty well, even nested arrays or plan 0 based index arrays. If you ever return JSON to your server, you can also use json_decode($return_data); to generate a php object from the returned data that you can use. Alternately, using json_decode($returned_data, TRUE); will modify the returned data back into a php associative array, so you have several options. I'll link the PHP doc references in my answer for these two functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.