Satch3000 Satch3000 - 9 months ago 65
Javascript Question

$.getJSON SyntaxError: missing ; before statement, why?

I am having a problem with some code. Here is what the JSON response looks like:


Here is the jQuery code:

$(function() {
$.getJSON('', function(data) {
// Create the chart
$('#container').highcharts('StockChart', {
rangeSelector: {
selected: 1
title: {
text: 'AAPL Stock Price'
series: [{
name: 'AAPL',
data: data,
tooltip: {
valueDecimals: 2

Here is the error I'm getting:

SyntaxError: missing ; before statement {"cars":"1","bikes":"1"}

What am I doing wrong here?


From the $.getJSON documentation:

If the URL includes the string "callback=?" (or similar, as defined by the server-side API), the request is treated as JSONP instead.

This is the cause of your problem as your return data is in JSON format, not JSONP. You just need to remove that property from the querystring of the request:

$.getJSON('', function (data) {
    // the rest of your code...