我创建了一个 php 来通过 json 编码从数据库返回分数。弗洛特似乎并不想要它们。firebug 中没有返回错误。所以我被困住了。我得到的只是一张空白图表-
编码..
<script type="text/javascript">
$(function() {
var options = {
bars: {
show: true
}
}
$.plot("#placeholder", data, options);
var iteration = 0;
function fetchData() {
++iteration;
function onDataReceived(series) {
data = [ series ];
$.plot("#placeholder", data, options);
}
$.ajax({
url: "http://localhost/charts/data",
type: "GET",
dataType: "json",
success: onDataReceived
});
if (iteration < 5) {
setTimeout(fetchData, 10000);
} else {
data = [];
alreadyFetched = {};
}
}
fetchData();
setTimeout(fetchData, 10000);
});
</script>
</head>
<body>
<div id="header">
<h2>AJAX</h2>
</div>
<div id="content">
<div class="demo-container">
<div id="placeholder" class="demo-placeholder"></div>
</div>
</div>
</body>
</html>
返回数据的php是
$data = array(array('1999',3.0),array('2000',3.9),array('2001',2.0),array('2002',1.2));
$array = array('label' => 'Scores','data'=>$data);
echo json_encode(array($array));
而返回的 Json 是
[{"label":"Scores","data":[["1999",3],["2000",3.9],["2001",2],["2002",1.2]]}]