3
votes

I am trying to create some JSON to be used for displaying a chart using Highcharts

http://www.highcharts.com/

I have copied one of their examples:

http://www.highcharts.com/stock/demo/basic-line

Click "View Options" under the graph to see the source. There is also a JSFiddle there to play with

If I copy that locally it all works fine.

The problem is when I try to use my own data source.

I have an ASP.Net MVC controler which is spitting out a list of arrays, just like their data source. However, that doesn't work.

Their datasource looks like this http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=?

and they retrieve it like this

$.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=?', function (data) {

So I thought I'd take a step back and copy thier data exactly and put it in a text file on my server and try that:

So I tried this

$.getJSON('/data.txt', function (data) {

and this

$.get('/data.txt', function (data) {

but neither work

I have also tried using both JSON.parse and jQuery.parseJSON after retrieving the data, but again - that doesn't seem to work

I am also wondering what the ? is at the start of their data

Their data looks like this

?([[<some data>],[some data]]);

I don't get any error message, the graph just doesn't display

any ideas?

SOLVED IT

Just need to retrive the data and turn it into an array and pass it to the chart.

Needs to be an array, not JSON

2
I have the EXACT same problem at the moment... I don't suppose you can give a little more info on how you solved it? Trying to integrate Highcharts in to a project and I can't get why their demo works and using my own doesn't... having problems understanding the other answers... +1'ed you.Wil

2 Answers

3
votes

That datasource is ouputting JSONP, which is for cross-domain AJAX requests. It's not valid 'raw' JSON because of that extra callback(...) wrapper.

Read up about it here: http://api.jquery.com/jQuery.ajax/ under the 'dataType' section.

2
votes

As you say in your tags, it's not JSON, it's JSONP. Do not parse it, catch it with a callback. Use jQuery.getScript to do it, and define function callback(data). Inside that function, data should contain the (parsed) object. Also, replace the ? in the URL with callback (or whatever you named your function) - ? is not a valid identifier in JavaScript, so ?([....]) is nonsense.