是否可以在单页结果集上获得两个json输出
我必须通过使用ajax将数据传递到另一个页面来填充数据网格和图表,并从单个mysql查询获取两种类型的json结果集,当我尝试返回json时,它无法处理
这是我的 result.php 代码,其中将生成json,
include('connection.php');
if (isset($_POST)) {
$rep_date1 = $_POST['date1'];
$date1 = date("Y-m-d", strtotime($rep_date1));
$rep_date2 = $_POST['date2'];
$date2 = date("Y-m-d", strtotime($rep_date2));
$sql = mysql_query("SELECT * FROM infra.prob_report WHERE prob_rept_date BETWEEN '$date1' AND '$date2'");
$rows = array();
while ($row = mysql_fetch_assoc($sql)) {
$nestedData = array();
$nestedData[] = $row["rep_id"];
$nestedData[] = $row["prob_rept_date"];
$nestedData[] = $row["prob_equip_name"];
$nestedData[] = $row["prob_rept_name"];
$nestedData[] = $row["prob_desc"];
$data[] = '<tr><td>'.$row["rep_id"].
'</td><td>'.$row["prob_rept_date"].
'</td><td>'.$row["prob_equip_name"].
'</td><td>'.$row["prob_rept_name"].
'</td><td>'.$row["prob_desc"].
'</td></tr>';
$point = array("label" => $row['prob_equip_name'], "y" => $row['rep_id']);
array_push($data_points, $point);
}
echo json_encode($data); //json output populating data-grid
echo json_encode($data_points); //json output populating chart
}
这是我的 handle.php 我的处理脚本运行的地方,
<script>
$(document).ready(function() {
$('#submit').click(function() {
var name = $("#name").val();
event.preventDefault();
$.ajax({
type: "POST",
url: "new_prob_submit.php",
data: {
'date1': $('#picker1').val(),
'date2': $('#picker2').val()
},
dataType: "json",
success: function(data) {
$('#tbdy').html(data);
$.getJSON("result.php", function(result) {
var chart = new CanvasJS.Chart("chartContainer", {
data: [{
dataPoints: result
}]
});
chart.render();
});
}
});
});
});
</script>
答案 0 :(得分:1)
做一件事。
在php中
echo json_encode(
array(
'data' => $data,
'dataPoints' => $data_points
)
); //json output populating data-grid and populating chart
在javascript中
success:function(result){
result.data;
result.dataPoints;
}