JSON.parse error on a seemingly valid JSON [duplicate]

I'm using JSON.parse() to parse a json that's being returned from an api (Laravel 5) called using jquery's $.get(). The json is seemingly valid, however, JSON.parse() is returning error in both Safari and Chrome.

Chrome says:

Uncaught SyntaxError: Unexpected token o

Safari says:

SyntaxError: JSON Parse error: Unexpected identifier "object"

The code fragment is as below:

    $.get('/foo/' + product_id, function(data){
        var product = JSON.parse(data);
        if (product) {
            // do something

The JSON is:

   "name":"Mr. Destin Hoppe",
   "created_at":"2015-09-16 17:39:34",
   "updated_at":"2015-09-16 17:39:34"

Interestingly, eval() works just fine.



The error is a result of data being an object, not JSON. You don't need to parse anything; it is already a JavaScript object. jQuery does the parsing within its get method. To confirm this, add this line to the top of the callback.


As another example of this error, the following line will also fail for the same reason.



Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.