exampleUsingPHP.htmlAjax和谷歌圖表。通過PHP
<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
var jsonData = $.ajax({
url: "getData.php",
//dataType:"java",
async: false
//success: function(results) {alert(results);}
}).responseText;
// Load the Visualization API and the piechart package.
google.load('visualization', '1', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
function drawChart() {
alert(jsonData);
// Create our data table out of JSON data loaded from server.
var data = google.visualization.arrayToDataTable(jsonData);
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, {width: 400, height: 240});
}
</script>
</head>
<body>
<!--Div that will hold the pie chart-->
<div id="chart_div"></div>
</body>
<script>
</script>
</html>
訪問getdata.php
<?php
echo "[['Task', 'Hours per Day'],
['Work', 11],
['Eat', 2],
['Commute', 2],
['Watch TV', 2],
['Sleep', 7]
]";
?>
所以我的問題是該行...
var data = google.visualization.arrayToDataTable(jsonData);
當我嘗試jsonData通入arrayToDataTable( ),它不喜歡它是一個字符串。我能做些什麼來將字符串「jsonData」轉換爲可用於arrayToDataTable()函數的有效javascript?
非常感謝你我的朋友。你解決了我的噩夢。 –
@JamesPiekarz肯定沒有問題 – user1978142