0

Help me please ...

I have two function and question - as to me to receive the chartData variable as AmCharts:

$.get(
    "/ITN/TEST/chart_test_JSON_REQUEST/",
    {
        param1:'title',
        param2:'BTSnum'
    },
    function(data) {
        var s = JSON.stringify(data);
        var chart;
        var chartData = s
        alert(chartData)    
    }
);

AmCharts.ready(function () {
    var chartData ????????????????????????????????

    var chart = new AmCharts.AmSerialChart();
    chart.dataProvider = chartData;

    chart.write('chartdiv');
});
qooplmao
  • 17,622
  • 2
  • 44
  • 69
  • 1
    Could you format your code please. And you question in unclear. – dcodesmith Jan 12 '14 at 09:30
  • 3
    Are you sure to want stringify the data you received via get? If you want to parse the JSON to make it a JavaScript type use `JSON.parse(data);` – idmean Jan 12 '14 at 09:34

2 Answers2

1

You should make use of JavaScript closures to get the data in the chartData variable using the callback function.

Note how in the callback the chartData variable is not preceded with a var statement. This means that the variable is not local and it references the same variable as the first chartData of the function.

AmCharts.ready(function () {
    var chartData;

    $.get(
        "/ITN/TEST/chart_test_JSON_REQUEST/",
        {
            param1:'title',
            param2:'BTSnum'
        },
        function(data) {
            chartData = JSON.Parse(data);
        }
    );

    var chart = new AmCharts.AmSerialChart();
    chart.dataProvider = chartData;

    chart.write('chartdiv');
});
Pang
  • 9,564
  • 146
  • 81
  • 122
astreal
  • 3,383
  • 21
  • 34
0

Excuse for the wrong formatting - when sending a question - already I understood and next time I will correctly format... (it is my first question)

AmCharts.ready(function () {
var chartData;

$.get(
    "/ITN/TEST/chart_test_JSON_REQUEST/",
    {
        param1:'title',
        param2:'BTSnum'
    },
    function(data) {
        chartData = JSON.Parse(data);
    }
);

var chart = new AmCharts.AmSerialChart();
chart.dataProvider = chartData;

chart.write('chartdiv');
});

I have errors :

chartData is undefined JSON.Parse is not a function

Al Foиce ѫ
  • 4,195
  • 12
  • 39
  • 49