2016-07-05 60 views
2

我遇到了一些代碼的問題。這裏是JSON響應的樣子:

{"cars":"1","bikes":"1"} 

這裏是jQuery代碼:

$(function() { 
    $.getJSON('https://myurlhere.com?filename=aapl-c.json&callback=?', function(data) { 
     // Create the chart 
     $('#container').highcharts('StockChart', { 
      rangeSelector: { 
       selected: 1 
      }, 
      title: { 
       text: 'AAPL Stock Price' 
      }, 
      series: [{ 
       name: 'AAPL', 
       data: data, 
       tooltip: { 
        valueDecimals: 2 
       } 
      }] 
     }); 
    });  
}); 

這是我得到的錯誤:

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

我在做什麼錯這裏?

回答

2

$.getJSON文檔:

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

這是因爲你的返回數據的問題的原因是JSON格式,而不是JSONP。您只需要從請求的查詢字符串中刪除該屬性:

$.getJSON('https://myurlhere.com?filename=aapl-c.json', function (data) { 
    // the rest of your code... 
});