Print return text json in middle of code - JQuery / JavaScript / JSON

1

I have a chart that is created according to the year, which is passed by AJAX and returns a JSON containing the values to create the chart. Well, I send AJAX with the year, I return the JSON, however ... It makes a mistake, it creates everything wrong, follow my code.

My controller where I return the data and create the JSON:

public function comparativo_json(){
    $html = '';
    $data = $this->relatoriosmodel->getComparativo($this->input->post('ano'));

    $html .= '[';
    foreach($data['mesesFuncionario'] as $func):
        $html .= "{";
            $html .= "name: '".$data['funcionarioDados'][$func['id']]->nome."',";
            unset($func['id']);
            $html .= "data: [ "; foreach($func as $f){ $html .= $f.','; } substr($f,-1); $html .= "]";
        $html .= "},";
    endforeach; 
    $html .= ']';

    echo json_encode($html);
}

My JS:

<script type="text/javascript">
$(function(){
    $('#ano').change(function(){
        $.ajax({
            url: '/relatorio/comparativo_json',
            type: 'POST',
            dataType: 'json',
            data: {ano: $("#ano option:selected").val()},
            success: function(data){

                $('#containerHighCharts').highcharts({
                    title: {
                        text: 'Comparativo de Vendas',
                        x: -20 //center
                    },
                    subtitle: {
                        text: 'Ano de Referência: '+$("#ano option:selected").val(),
                        x: -20
                    },
                    xAxis: {
                        categories: ['Jan', 'Fev', 'Mar', 'Abr', 'Mai', 'Jun',
                            'Jul', 'Ago', 'Set', 'Out', 'Nov', 'Dez']
                    },
                    yAxis: {
                        title: {
                            text: 'Reais (R$)'
                        },
                        plotLines: [{
                            value: 0,
                            width: 1,
                            color: '#808080'
                        }]
                    },
                    tooltip: {
                        valueSuffix: ' (R$) Reais'
                    },
                    legend:{
                        layout: 'vertical',
                        align: 'right',
                        verticalAlign: 'middle',
                        borderWidth: 0
                    },
                    series: data,
                });
            }
        });
    });
}); 
</script>

The JSON that it returns, which by the way is correct, according to the pattern requested by the plugin:

[
    {
        name: 'Richard Feliciano',
        data: [ 0,0,0,0,1816,17100,2400,0,0,0,0,0,]
    },
    {
        name: 'Ewerton Melo',
        data: [ 0,0,0,0,0,12400,0,0,0,0,0,0,]
    },
]

Image containing the error:

I just need to make the data return print correctly.

    
asked by anonymous 20.06.2014 / 00:11

1 answer

1

There is a problem here:

foreach($data['mesesFuncionario'] as $func):
    $html .= "{";
        $html .= "name: '".$data['funcionarioDados'][$func['id']]->nome."',";
        unset($func['id']);
        $html .= "data: [ "; foreach($func as $f){ $html .= $f.','; } substr($f,-1); $html .= "]";
    $html .= "},";
endforeach;

The json string format can not have a comma at the end, it only separates the objects. Here is the example you gave:

[{name: 'Richard Feliciano',data: [ 0,0,0,0,1816,17100,2400,0,0,0,0,0,]},{name: 'Ewerton Melo',data: [ 0,0,0,0,0,12400,0,0,0,0,0,0,]},]

It should be:

[{name: 'Richard Feliciano',data: [ 0,0,0,0,1816,17100,2400,0,0,0,0,0,]},{name: 'Ewerton Melo',data: [ 0,0,0,0,0,12400,0,0,0,0,0,0,]}]

Notice that I removed the last comma. I do not understand PHP, but what you need to do is check it so it does not add the last comma.

After correcting this, just use JSON.parse (date); which will work perfectly.

    
20.06.2014 / 01:02