I keep getting "Uncaught SyntaxError: Unexpected token o" I keep getting "Uncaught SyntaxError: Unexpected token o" json json

I keep getting "Uncaught SyntaxError: Unexpected token o"


Looks like jQuery takes a guess about the datatype. It does the JSON parsing even though you're not calling getJSON()-- then when you try to call JSON.parse() on an object, you're getting the error.

Further explanation can be found in Aditya Mittal's answer.


The problem is very simple

jQuery.get('wokab.json', function(data) {    var glacier = JSON.parse(data);});

You're parsing it twice. get uses the dataType='json', so data is already in json format.Use $.ajax({ dataType: 'json' ... to specifically set the returned data type!


Basically if the response header is text/html you need to parse, and if the response header is application/json it is already parsed for you.

Parsed data from jquery success handler for text/html response:

var parsed = JSON.parse(data);

Parsed data from jquery success handler for application/json response:

var parsed = data;