想要提醒第二个json数据阵列

时间:2016-01-01 18:19:28

标签: javascript php arrays json ajax

在这里,我得到两个json数组。 我在第一个json数据之后退出,以便在ajax中发出警报。 但是当我要提醒第二个json数据时,它显示“未定义”。 那么,第二个json数据的警报值怎么样?

我的代码就像,

$data1['month_result'] = $user_wise_performance;
$data1['total_point'] = $total_point;
$data1['total_earn_point'] = $total_earn_point;
echo json_encode($data1);
exit();
$data2['week_month_result'] = $user_wise_performance;
$data2['week_total_point'] = $total_point;
$data2['week_total_earn_point'] = $total_earn_point;
echo json_encode($data2);
exit();

ajax调用就好,

jQuery.ajax({
    url: "<?php echo base_url(); ?>grade_tasks/emp_performance",
    data:'',
    type:"GET",
    dataType: "json",
    success:function(data){

        alert(data.total_earn_point);
        alert(data.week_total_earn_point);  //This is not printing the value.

    },
    error:function (){}
});

(更新): 在这里,如果我没有调用exit(),我没有在ajax中获取值, 那可能是什么问题?

1 个答案:

答案 0 :(得分:1)

你应该删除第一个exit();或删除这两个,我猜你的代码应该是这样的:

$data1['month_result'] = $user_wise_performance;
$data1['total_point'] = $total_point;
$data1['total_earn_point'] = $total_earn_point;
$data1['week_month_result'] = $user_wise_performance;
$data1['week_total_point'] = $total_point;
$data1['week_total_earn_point'] = $total_earn_point;

echo json_encode($data1);

或者尝试使用多维数组,如下面的评论中提到的 @Ohgodwhy

$data = 
[
    'data1' => [ 
        'month_result' => $user_wise_performance,
        'total_point' => $total_point,
        'total_earn_point' => $total_earn_point
    ],
    'data2' => [ 
        'week_month_result' => $user_wise_performance,
        'week_total_point' => $total_point,
        'week_total_earn_point' => $total_earn_point
    ]
];

echo $data;

希望这有帮助。