1

I have an error in Javascript

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
...n.now(),dc=/\?/;n.parseJSON=function(a){return JSON.parse(a+"")},n.parseXML=func...

And this is my JavaScript, which gets string from server response

complete: function(response) {
    var results = $.parseJSON(response.responseText);

Error is in the second line because without that line there's no error)

My PHP script: (I use CodeIgniter)

    $str = json_encode($results);
    $this->output->set_output($str);

And an example of the message from server:

{"Thumb_image":"http:\/\/example.com\/img\/thumbs\/9b4138094cb32af906e32f9d033d4748.jpg","Big_image":"http:\/\/example.com\/img\/photos\/9b4138094cb32af906e32f9d033d4748.jpg","Error":""}

Help me please. Why this code is working wrong?

3
  • 2
    Nope, I don't buy it. response.responseText is not what you're telling us it is - console.log it. Commented Aug 30, 2014 at 17:13
  • @BenjaminGruenbaum: I can bet the user is using jQuery 1.9+ and the user is passing undefined to $.parseJSON. Commented Aug 31, 2014 at 0:25
  • possible duplicate of SyntaxError: JSON.parse: unexpected character Commented Aug 31, 2014 at 0:32

1 Answer 1

0

Try this

 $str = json_encode($results);
 $this->output->set_header('Content-Type: application/json; charset=utf-8');
 echo $str;
Sign up to request clarification or add additional context in comments.

3 Comments

Why should the OP try this? How will it solve the problem? Please provide an explanation so that other readers can learn.
I can't use such function as header, because CodeIgniter doesn't allow me that. It writes in error log that headers were already sent. Maybe someone has ideas how can I do this by using CodeIgniter means?
This helped. Thank you. I simply had some more bugs

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.