1

Running an issue with parsing JSON recieved from php backend. On php i have an array, which i send by json_encode:

$result[] = (object) array('src' => "{$mergedFile}", 'thumb_src' => "{$thumb_file}");
echo json_encode($result);

And when trying to JSON.parse it i recieve error:

"Uncaught SyntaxError: Unexpected token F"

The response itself looks this like:

[{"src":"upload\/lessons\/963\/video\/176481500-m.webm","thumb_src":"upload\/lessons\/963\/slide\/thumb_0f515a62753626e1aaefdc7968e8103e.jpg"}]

Very strange thing is that, similar code works nearby.. looks fine.. Appreciate any help, thanks.

4
  • You JSON is fine, the PHP code you have on the backed probably isnt. Commented Jul 2, 2015 at 7:01
  • 2
    can you show us the JSON.parse part ? Commented Jul 2, 2015 at 7:05
  • nothing special there, i just do JSON.parse(data) ... the data i recieve in request's callback Commented Jul 2, 2015 at 7:20
  • I've solved this using regexps, but i still would like to hear the correct answer, thx. Commented Jul 2, 2015 at 7:33

1 Answer 1

2

You cannot JSON.parse() JSON, the param must be a string :

JSON.parse('[{"src":"upload\/lessons\/963\/video\/176481500-m.webm","thumb_src":"upload\/lessons\/963\/slide\/thumb_0f515a62753626e1aaefdc7968e8103e.jpg"}]');

the error you receive can easily be reproduced by :

JSON.parse([{"src":"upload\/lessons\/963\/video\/176481500-m.webm","thumb_src":"upload\/lessons\/963\/slide\/thumb_0f515a62753626e1aaefdc7968e8103e.jpg"}]);
Sign up to request clarification or add additional context in comments.

1 Comment

You're right, but strange. Look, i receive data in callback of request. Typeof(data) == string, data is the same as response, but i cannot do parse, why?

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.