我想制作一个Google图表,我试图从一个PHP脚本发送一个JSON编码数组到一个JavaScript。这两个文件如下:如何将数组从PHP传递到使用JSON生成Google图表的JavaScript?
<html>
<head><title>
</title></head>
<body>
<?php
$data[0]=array('State','Values');
$data[1]=array('Correct',6);
$data[2]=array('Incorrect',3);
echo json_encode($data);
?>
</body>
</html>
这被保存为test.php的
JavaScript文件是:
<html>
<head>
<script type="text/javascript" src="jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var jsonData = $.ajax({
url: "test.php",
dataType: "json",
async: false
}).responseText;
var data=google.visualization.arrayToDataTable(jsonData);
var options = {
title: 'My Chart'
};
var chart = new oogle.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
<body>
<div id="chart_div" style="width: 900px; height: 500px;"></div>
</body>
</html>
这被保存为test2.php。
当我运行test2.php时,没有任何反应,浏览器显示一个空白屏幕。我在哪里错了?请帮忙。
谢谢。
谢谢你,我的朋友。你救了我的命。你是一个传奇! – 2013-04-20 17:36:47
如果这解决了您的问题,请在我的答案旁边的绿色勾号中打勾 - 欢迎来到Stack Overflow! – Alfo 2013-04-20 18:47:54