0

I have a JSON array stored in a file data.json - I used the code that was recommended here for a similar problem, explain why when I try to access the array later in the program it tells me it is undefined.

    var chartData = (function () {
                var chartData = null;
                $.ajax({
                    'async': false,
                    'global': false,
                    'url': "data.json",
                    'dataType': "json",
                    'success': function (data) {
                        chartData = data;
                    }
             });
             return chartData;
        })();

Help appreciated!

Edit; For accessing, I'm using the array as a series in dojo-chart: http://dojotoolkit.org/grids-charts

After I attempt to initialize var chartData, the only other point I use it is in:

    chart.addSeries("Name",chartData);

Edit #2; In case it matters the data.json file is formatted like:

    [#, #, #, ... , #]
Drew
  • 21
  • 6
  • Show us how you're trying to access the array. Better yet, show us a working example if you can. – squidbe Jun 01 '11 at 21:11
  • Have you echoed the `data` that is returned in the success handler to verify that you are getting the data you expected, not something like a 404 page served with a 200 code by a misconfigured web server, etc? – Useless Code Jun 02 '11 at 19:18

2 Answers2

1

I guess it should work somehow as you set async: false. But better is to use it asynchronously:

function handler(chartData) {
    // all the code here that deals with chartData
}

$.ajax({
    url: "data.json",
    dataType: "json",
    success: handler
});
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
1

Perhaps because it was not successful? I found it very useful to debug using console.log (firebug, or most modern web developer panels in chrome or ie support it)

you can try this piece of code to debug it.

var chartData = (function () {
            var chartData = null;
            $.ajax({
                'async': false,
                'global': false,
                'url': "data.json",
                'dataType': "json",
                'success': function (data) {
                    console.log('success');
                    console.log(data);
                    chartData = data;
                }
                error: function (data) { console.log('error'); console.log(data); },
         });
         return chartData;
})();
Arend
  • 3,741
  • 2
  • 27
  • 37
  • Can you verify that the server-side script is working? For example by tracing the error in firebug? Another common error is that your json is in fact invalid json. – Arend Jun 01 '11 at 22:34
  • And what would invalid json look like, and how would I distinguish that from valid json. – Drew Jun 02 '11 at 13:13
  • @Draw: Put your JSON here: http://jsonlint.com/ Have a look at the JSON specification to learn what the correct syntax is: http://json.org/ – Felix Kling Jun 02 '11 at 13:16
  • @Drew you may also change the part of dataType, to "text", to evaluate if the error is in the json parsing part. Otherwise, you may try to access the php part synchronously or investigate with Firebug (or equivalent). Do you have a link to show the error? – Arend Jun 02 '11 at 14:29
  • Thanks for the help, apparently it was a problem with Chrome accessing local files like in this question: http://stackoverflow.com/questions/2541949/problems-with-jquery-getjson-using-local-files-in-chrome – Drew Jun 02 '11 at 19:39