【发布时间】:2016-09-11 06:54:47
【问题描述】:
当我尝试在视图页面上使用返回的数组数据时,它会打印未定义的非数组值。请帮帮我。
我的控制器是:
function test(){
$pro_id = $this->input->post('id',TRUE);
//echo $pro_id;
$cat_id = $this->input->post('cat',TRUE);
$date1 = $this->input->post('dt',TRUE);
$firstdate = date('Y-m-d', strtotime($date1));
$lastdate = strtotime(date("Y-m-d", strtotime($firstdate)) . " +6 day");
//echo $cat_id;
$data = array();
$this->load->model('inserttimemodel');
$whr = $this->inserttimemodel->gettime($pro_id,$cat_id,$firstdate,$lastdate);
$data['whr'] = $whr;
//print_r($data); //working properly
echo json_encode($data);
exit();
}
JS:
$(document).ready(function(){
$("#cat").change(function(){
var catid = $(this).val();
var id = $('#project option:selected').val();
var dt = $('#period option:selected').val();
$.ajax({
url:"<?php echo base_url();?>/timesheet/test",
data:{cat:catid,id:id,dt:dt},
type:"POST",
dataType: "json",
success:function(data){
document.getElementById("res").innerHTML = data[0];
document.getElementById("res1").innerHTML = data[1];
}
});
});
});
【问题讨论】:
-
尝试通过在成功回调中添加 console.log(data) 语句来诊断代码,然后检查浏览器的控制台。
-
查看您的 PHP,您的数据应该位于 data.whr 中。
-
数组 ( [whr] => 数组 ( [0] => 数组 ( [whr] => 1 ) [1] => 数组 ( [whr] => 2 ) [2] =>数组 ( [whr] => 3.3 ) [3] => 数组 ( [whr] => 6 ) [4] => 数组 ( [whr] => 5 ) [5] => 数组 ( [whr] => 8 ) [6] => 数组 ( [whr] => 5:30 ) ) )
标签: javascript arrays ajax