I have the following code:
$.ajax({
async: false,
url: 'chart_data.php',
data: {'option':'high', 'id':id},
dataType: 'json',
success: function(response){
alert('in');
var data = google.visualization.arrayToDataTable(response);
var options= { curveType:'none', width: 300, height: 200, hAxis: {title:'Years'}, vAxis: {title:'Value'}, title: 'High Time - Low Value' };
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
});
in a webpage. I'd like the chart to display, based on a drop-down menu. The rest of the function works just fine. I checked in Firebug, and the JSON response comes through with a 200 OK status message. In fact, I can see all the JSON just fine in Firebug.
The question remains, why does the alert('in');
never get triggered, and why doesn't google load it's chart into the associated chart div?
Edit:
I get the following error when adding an error callback: parseerror SyntaxError: JSON.parse: unexpected character.
This is the JSON response from the server:
[['Year', 'Low', 'High'],['1984', 318000, 395000],['1984', 418000, 495000],
['1984', 380000, 450000],['1984', 410000, 460000],['1984', 410000, 460000],
['1985', 435000, 485000],['1985', 435000, 485000],['1985', 435000, 485000],
['1985', 435000, 485000],['1985', 435000, 485000],['1985', 435000, 485000],
['1985', 435000, 485000],['1985', 435000, 485000],['1985', 318000, 395000],
['1985', 418000, 495000],['1985', 380000, 450000],['1985', 420000, 470000],
['1985', 420000, 470000]]
It looks correct to me, although I am not 100% certain on JSON structure.
Edit:
I have made a few changes, and it seems that I am getting a little further along. First, I removed the header('Content-type: application/json');
line from my php script. I then changed the dataType: 'json'
to dataType: 'html'
.
It now loads the success callback in the $.ajax() call. It doesn't like the text response when I try to send that to Google's arrayToDataTable() method.
If I can get the returned string (which looks like the above JSON response that isn't actually a JSON response) to be parsed into a javascript array, then I'd be golden. I hope.
Edit:
I ended up using an eval statement in my success callback to turn the http response into an array eval('var res = ' + response);
This is probably not the best way to do this. If someone has a better (and more secure) way of doing this, that'd be great.
Edit:
I didn't really like using the eval statement, so I looked into other ways of changing the return string into an array. I didn't really care for any of them until I ran across this little gem:
response = JSON.parse(response);
This works without issue.
Thanks for everyone's assistance.