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 the AJAX with the year, return the JSON, however... It gives error, 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 it returns, which by the way is correct, as 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 return data
be printed correctly.
The problem is specifically in the impression of
series: data,
– Ewerton Melo
The only thing I can think of is that I’m forgetting to do the JSON.parse, 'Cause otherwise it feels good: http://jsfiddle.net/yb3Nt/ Test like this:
series: JSON.parse(data),
Test alsoconsole.log(data);
orconsole.log(typeof data);
and comment here on what you get.– Sergio
I did the
console.log(typeof data);
and he returns asstring
, just below did the parsevar dataJSON = JSON.parse(data)
and returned error:Uncaught SyntaxError: Unexpected token n
– Ewerton Melo
In fact it must be the JSON conversion of this string
– Ewerton Melo
Okay, and what you get if you do
console.log(data);
?– Sergio
[{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,]},]
– Ewerton Melo
It returns the pattern of the JSON straight, but as string. I tried
JSON.parse(data)
or$.parseJSON(data)
and the error is the same as in the above comment...– Ewerton Melo
And if you stop to analyze, I give json_encode() right at the return of the value.
– Ewerton Melo
I also tried the
JSON.stringify(data)
that as I looked in some forums also serves to convert string into JSON, however, it keeps as string. I did straight only to testconsole.log(typeof JSON.stringify(data));
but still continues as string.– Ewerton Melo
Either you create an array/object and use json_encode() or you create a string and do not use Encode. In the case of the second option (which is what you are doing), quote
"
around the key, and value that is string, of each object. And remove the extra commas. Then this string will be accepted by JSON. Take a look here: http://jsfiddle.net/n3ARn/– Sergio
@Sergio, thank you so much for your help, but the tips below solved the problem. Thank you so much for your willingness to help. A hug.
– Ewerton Melo