1

I'm sure this is probably me being stupid but I want to confirm this issue. I am building a windows 8 app and loading in data from a mysql server and I can't seem to fix this issue. From a bit of reading I am guessing the issue is that the code is executing before the json request is this correct and is there a way to resolve it. The return is there because it is inside a function which returns the data and then displays it. The "SampleItems[0]" displays perfectly but the stuff inside the json wont but a console log shows it is getting data from the server. Thanks in advance for any help!

    var sampleItems = [];
    sampleItems[0] = { group: sampleGroups[count], title: "New Image", subtitle: "", description: "", content: "", backgroundImage: "/images/add.png" };

        //this code calls to our cloud based service which gets the data which is needed and returns it in a JSON object back for this to handle
        $.getJSON('http://pumpstationstudios.com/tumapics/services/getimages.php', function (data) {
            var service_url = 'http://pumpstationstudios.com/tumapics/';
            var count = 1;


            images = data.items;
            $.each(images, function (index, image) {
                image.image = service_url + image.image;
                sampleItems[count] = { group: sampleGroups[count], title: "Hello", subtitle: "", description: "", content: "", backgroundImage: image.image };
                count++;
            });
        });

    return sampleItems;

1 Answer 1

1

As it happens, I answered a similar question javascript code execution and ajax async some hours ago

Essentially, what happens is

$.getJSON('http://.../getimages.php', function(data) {
// process data
});

// this runs *before* "process data"
return sampleItems;

This means, you return sampleItems, before it is filled by the getJSON callback function.

To solve this problem, you must do your processing in the callback function.

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.