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:

    [#, #, #, ... , #]
2
  • Show us how you're trying to access the array. Better yet, show us a working example if you can. Commented Jun 1, 2011 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? Commented Jun 2, 2011 at 19:18

2 Answers 2

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;
})();
Sign up to request clarification or add additional context in comments.

5 Comments

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.
And what would invalid json look like, and how would I distinguish that from valid json.
@Draw: Put your JSON here: jsonlint.com Have a look at the JSON specification to learn what the correct syntax is: json.org
@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?
Thanks for the help, apparently it was a problem with Chrome accessing local files like in this question: stackoverflow.com/questions/2541949/…
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
});

1 Comment

Tried this it didn't seem to change anything.

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.