0

I am creating new functionality where I build a grid based on Json data returned from Ajax. I have decided I want to encapsulate this functionality within a function, so when I add/update/delete, I can on success retrieve a new representation of the data.

The problem I am having is I want to fill a global array but once my function that uses AJAX ends, I have an Array but no data. This isn't a problem when all the code is within the AJAX call but once I try to separate this in to its own function, it doesn't work as intended.

 <script type="text/javascript">
    var DataArray = [];

    // Use this function to fill array
    function retrieveNotes() {
        $.ajax({
            url: "http://wks52025:82/WcfDataService.svc/GetNotesFromView()?$format=json",
            type: "get",
            datatype: "json",
            asynch:true,
            success: function (data) {
                returnedData = data;
                $.each(data.d, function (i, item) {
                    DataArray[i] = [];
                    DataArray[i][0] = item.NotesTitle.trim();
                    DataArray[i][1] = item.ProfileName.trim();
                    DataArray[i][2] = item.IsShared;
                    DataArray[i][3] = item.NameOfUser.trim();
                }) // End of each loop
            }
        });
    }


    $(document).ready(function () {
        retrieveNotes();
        DataArray;
    </script>
3
  • 2
    1. asynch:true, --- async 2. read what it means in jquery documentation Commented May 15, 2013 at 11:40
  • it is because ajax is asynchronous, search for ajax callback Commented May 15, 2013 at 11:40
  • Typo here: $(document).ready(function () { retrieveNotes(); DataArray; </script> Commented May 15, 2013 at 11:41

1 Answer 1

5

It's asynchronous, so you'll have to wait for the ajax call to finish before you can use the data :

function retrieveNotes() {
    return $.ajax({
        url: "http://wks52025:82/WcfDataService.svc/GetNotesFromView()?$format=json",
        type: "get",
        datatype: "json"
    });
}

$(document).ready(function () {
    retrieveNotes().done(function(data) {
        var DataArray = [];
        $.each(data.d, function (i, item) {
            DataArray[i] = [];
            DataArray[i][0] = item.NotesTitle.trim();
            DataArray[i][1] = item.ProfileName.trim();
            DataArray[i][2] = item.IsShared;
            DataArray[i][3] = item.NameOfUser.trim();
        });
        // you can only use the data inside the done() handler,
        // when the call has completed and the data is returned
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

@adeneo I will give your solution a try.

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.