Hi,
I'm fairly new to javascript, PHP, and SQL, and have been having trouble
getting my code to work. After searching for similar problems here and on
stackexchange, I haven't been able to fix it and thought I would post here
in hopes that someone could help.
I'm trying to get some data from a MySQL database "myTest", convert it into
JSON format, and return it to draw an annotated timeline. When I test my
files in chrome, the console gives me the error "Uncaught SyntaxError:
Unexpected token <" at the line var data = new
google.visualization.DataTable(JSON.parse(jsonData));. In trying to fix the
issue, I believe the problem is likely that the PHP code is either
incorrect, or the return is not in JSON format, but I am unsure how to see
the output of the PHP to check what it looks like.
Any help would be greatly appreciated, and I can try to provide any other
details that may be important in fixing the problem. My PHP code, and a
snippet of my javascript code is below.
Thanks
Querydata.php:
<?php
$con = mysql_connect('localhost', 'root', 'password') or die('Error
connecting to server');
mysql_select_db('myTest', $con);
$query = mysql_query('SELECT price, date FROM myTest');
$table = array();
$table['cols'] = array(
array('label' => 'price', 'type' => 'number'),
array('label' => 'date', 'type' => 'date')
);
$rows = array();
while($r = mysql_fetch_assoc($query)) {
$temp = array();
$temp[] = array('v' => $r['price']);
$temp[] = array('v' => $r['date']);
$rows[] = array('c' => $temp);
}
$table['rows'] = $rows;
// encode the table as JSON
$jsonTable = json_encode($table,JSON_NUMERIC_CHECK);
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
header('Access-Control-Allow-Origin: *');
// return the data
echo $jsonTable;
?>
Test.js:
function drawChart() {
var jsonData = $.ajax({
url: "http://localhost:8000/Desktop/Querydata.php",
dataType:"json",
crossDomain : true,
async: false
}).responseText;
var data = new google.visualization.DataTable(JSON.parse(jsonData));
var chart = new
google.visualization.AnnotatedTimeLine(document.getElementById('chart_div'));
chart.draw(data, {displayAnnotations: true});
};
--
You received this message because you are subscribed to the Google Groups
"Google Visualization API" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-visualization-api.
For more options, visit https://groups.google.com/groups/opt_out.