我有一个 php 页面(array.php)。在浏览器上,array.php 产生 ff 结果
[{"name":"London","data2":["70","19"]},{"name":"Tokyo","data2":["60","18"]}]
array.php 页面
<?php
//header("Content-type: text/json");
header("Content-type: application/json");
$db = mysql_connect("localhost", "username", "userpasswd");
mysql_select_db("weather",$db);
$query = "SELECT * FROM measures";
$result = mysql_query($query,$db);
while($row = mysql_fetch_array($result))
{
$h = $row["humidity"];
$w = $row["windspeed"];
$t = $row["temperature"];
$c = $row["city"];
$ar1[] = array("name" =>$c,"data2"=>[$h,$t]);
}
echo json_encode($ar1);
?>
jquery页面的代码是
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="./js/highcharts.js"></script>
<script type="text/javascript">
var chart = null; // global
$(document).ready(function() {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
defaultSeriesType: 'column',
events: {
load: requestData
}
},
title: {
text: 'Real time data from database'
},
xAxis: {
categories: []
},
yAxis: {
minPadding: 0.2,
maxPadding: 0.2,
title: {
text: 'Value',
margin: 80
}
},
series: []
});
});
/**
* Request data from the server
*/
function requestData() {
$.ajax({
url: 'array.php',
success: function(point) {
$.each(point, function(i,item){
var series_name = item.name;
var series_data = item.data2;
var series = {data: []};
chart.xAxis.categories.push(series_name);
//chart.series.data.push(item.data2);
$('body').append( "Name: " + series_name);
alert(series_data);
});
},
cache: false
});
}
</script>
</head>
<body>
<div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div>
</body>
</html>
虽然代码不完整。但在错误修复之前,我无法继续前进。如果我运行 html 代码,alert 函数通过循环并警告来自 array.php 的每个数据来很好地工作。但是如果我取消注释
//chart.xAxis.categories.push(series_name); or //chart.series.data.push(item.data2);
然后警报功能不再起作用。我有一种感觉,它无法识别变量(图表)。我之前已经声明过
$(document).ready(function() {
所以它将是一个全局变量,但似乎并非如此。
拜托,我真的需要帮助。我已经为此工作了几天,但没有成功。我将深深感激。提前致谢 - 艾玛