这是我的ajax请求代码
function ajaxRequest() {
var currentIndexDay = $('#fieldValue').text();
var currentIndexMonth = $('#monthFieldValue').text();
var companyG = $("#companyGname").val();
var timeline = $("#timeL").val();
var month = $("#monthSelect").val();
var year = $("#yearSelect").val();
var dateRange = $("#dateR").val();
$.ajax({
method: "POST",
url: "<?php echo base_url(); ?>index.php/admin/home/pie_chart",
data: { day: currentIndexDay, week: currentIndexMonth, companyGroup: companyG, timeline: timeline, month: month, year: year, daterange: dateRange}
}).done(function( msg ) {
$("#test").html(msg);
});
}
在完成功能上,收到的数据将在div上显示,id是 test ,它是从控制器返回的
这是我的控制器
public function pie_chart() {
$data = $this->Statisticsmodel->pie_data_all_day($day);
$ap = array();
if(!empty($data)) {
foreach($data as $d=>$value){
if($value->moodstatus == 1 ){
$ap['Ok'] = $value->total;
}elseif($value->moodstatus == 2) {
$ap['Bad'] =$value->total;
}elseif($value->moodstatus == 0) {
$ap['Good'] =$value->total;
}
}
}
echo json_encode($ap);
}
那么我如何能够在其他地方捕获输出,而不仅仅是在div
中答案 0 :(得分:2)
您需要将控制器功能更改为:
public function pie_chart() {
$day= $this->input->post('day') // fetch the post data
$data = $this->Statisticsmodel->pie_data_all_day($day);
$ap = array();
if(!empty($data)) {
foreach($data as $d=>$value){
if($value->moodstatus == 1 ){
$ap['Ok'] = $value->total;
}elseif($value->moodstatus == 2) {
$ap['Bad'] =$value->total;
}elseif($value->moodstatus == 0) {
$ap['Good'] =$value->total;
}
}
}
echo json_encode($ap);
}
由于未提取日期,因此$data
为空并且您获得空响应。
希望这会有所帮助。
答案 1 :(得分:0)
更改您的ajax功能,如
function ajaxRequest(getData) {
var currentIndexDay = $('#fieldValue').text();
var currentIndexMonth = $('#monthFieldValue').text();
var companyG = $("#companyGname").val();
var timeline = $("#timeL").val();
var month = $("#monthSelect").val();
var year = $("#yearSelect").val();
var dateRange = $("#dateR").val();
$.ajax({
method: "POST",
url: "<?php echo base_url(); ?>index.php/admin/home/pie_chart",
data: { day: currentIndexDay, week: currentIndexMonth, companyGroup: companyG, timeline: timeline, month: month, year: year, daterange: dateRange},
success:function(data) {
getData(data);
}
});
}
在div中使用它
ajaxRequest(function(output){
$("#test").html(output);
});
在图表中使用
ajaxRequest(function(output){
$('#piechart').piechart({
data : output
});
});