0

I get an variable via ajax from php file

This is data for google column chart

When i use it for build chart - it doesn't work

But same data works fine, when i paste it directly

Here example:

$.get("./get_charts.php", function(data){
   //alert(data.daily) = [ ["12.10.2011", 2250],["07.12.2011", 100] ]
   drawChart('daily', 'number', 'Kgs', data.daily, 0);
}, "json"); 
}

function drawChart(ctype, col_type, col_name, cdata, baseline) 
{       
   var data = new google.visualization.DataTable();
    data.addColumn('string', 'Date');
    data.addColumn(col_type, col_name);
    //doesn't work
    data.addRows(cdata);
   //works fine
    data.addRows([ ["12.10.2011", 2250],["07.12.2011", 100] ]);
   ....
}

I guess it's because this variable is passed as string value, but not array

How to convert it?

3
  • 1
    api.jquery.com/jQuery.parseJSON Commented Jan 16, 2012 at 13:29
  • I think we would have to see what "data.daily" is. Can you tell us or do a console.log on it and paste the output? Commented Jan 16, 2012 at 13:29
  • when you give "json" as data type it won't be string Commented Jan 16, 2012 at 13:32

2 Answers 2

1

Have you tried using the function eval?

var result = eval(data);

or jquery has a parseJSON function.

this online parser uses both ways to get a json string to a variable:

http://json.parser.online.fr/

Sign up to request clarification or add additional context in comments.

Comments

0

Make sure that content type is application/json

then use

$.getJSON("./get_charts.php", function(data){
   //alert(data.daily) = [ ["12.10.2011", 2250],["07.12.2011", 100] ]
   drawChart('daily', 'number', 'Kgs', data.daily, 0);
});

If content type is correct your code should also work. If it is coming as string, @Richard's solution will work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.