我正在尝试绘制一个来自我正在使用 getdata.php?id=allTemp 查询的 sql 数据库的 highcharts 图表。我对 JSON 不太了解,但到目前为止我已经组装了下面的代码,图表甚至没有绘制。$.getJSON 函数在我获得一个变量(例如 weather.wesleyweisenberger.com/getdata.php?id=humity)时起作用,但是一旦我尝试获取 getdata.php?id=allTemp 它就不起作用,请帮忙我...
while($data = $result->fetch_assoc()){
echo json_encode($data, JSON_FORCE_OBJECT);
}
并且返回格式为
{"timestamp":"2018-05-30 00:33:05","temperature":"67.39","humidity":"66.57","pressure":"99.21"}{"timestamp":"2018-05-30 00:47:39","temperature":"65.52","humidity":"70.41","pressure":"99.2"}{...
我的 index.php 文件显示:
<script>
$(function(){
$.getJSON('getdata.php?id=allTemp', function(json){
$('#container').highcharts({
series: [{
data: json
}],
title: {
text: 'Title'
}
})
})
})
</script>
<div id="container" style="width:100%; height:400px;"></div>
{...}{...}{...}
这是一个无效的 json 模式。你得到这个是因为你正在回显多个 json 对象,其中响应需要一个。您可以仅通过回显一次来纠正此问题。
$results = array();
while($data = $result->fetch_assoc()){
array_push($results, $data);
}
echo json_encode($results);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句