1

showArray is a function witch show the content of json file, what's wrong in my function? why it doesn't work

function getArray() {
    var quest = [];
    $.getJSON('data.json', function (json) {
        for (var key in json) {
            if (json.hasOwnProperty(key)) {
                var item = json[key];
                quest.push({
                    Name:item.Name
                });
            }
        }
        callback(quest);
    });
}
$(document).ready(function () {
    getArray();
});
function showArray() {
    var callback = function (quest) {
        console.log(quest)
    }
}
3
  • that's should to works.. Commented Feb 17, 2013 at 19:14
  • Are you expecting the callback inside showArray to be called? It is scoped inside showArray so getJSON cannot access it. Commented Feb 17, 2013 at 19:15
  • Don't use document.write(). Commented Feb 17, 2013 at 19:15

1 Answer 1

1

callback is a local variable. You need to get it to the getArray() function, probably by passing it as a parameter.

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

Comments

Your Answer

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