views:

1331

answers:

3

Hi, I have this code:

TheObject = {

    getArray: function(){
        var groups = new Array;
        $.ajax({
              type: "POST",
              url: "link.php",
              success: function (data){
                  var counter = 0;
                  $('g',data).each(function(){    
                      var group_name = $(this).find("name").text();
                      var group_id = $(this).find("id").text();
                      var group = {
                         id: group_id,
                         name: group_name
                      }
                      groups[counter] = group;
                      counter++;
                  });
                  return groups;
              }
         });
     }

}

And when I try to call this method:

var a = TheObject.getArray();
alert(a);

It returns 'undefined'. I cant figure out where is the problem. The array gets created inside the success function but I'am unable to return it properly. Thanks for your help!

A: 

Use push on the array. Also you want to create a type called Group and then create a new group in the loop and then push it into the array.

Teja Kantamneni
Please read the question. I don't have a problem with creating the array. The problem is I don't know how to get it outside of the success function.
ecu
A: 

AJAX calls are asynchronous. The "success" callback is called only after the request completes, which in this case is after the execution of the getArray() function. I'm not sure if it's possible to return a value from a callback; I haven't tried it. You can try setting "async:false", in your jQuery AJAX call, which ensures that execution pauses until the AJAX request completes.

Instead of having it return the value, I think it's better to use a global (namespaced, ideally) variable.

Vivin Paliath
A: 

In your code, you are looking for groups using procedural coding after the ajax call was made. The main problem is that you are looking for groups before the ajax call is complete.

Another problem is that you are returning groups to the success() function, but the TheObject.getArray() function returns nothing.

So you need to bring in the callback into the ajax function like this:

TheObject = {
    getArray: function(callback) {
        var groups = new Array;
        $.ajax({
              type: "POST",
              url: "link.php",
              success: function (data){
                  var counter = 0;
                  $('g',data).each(function(){    
                      var group_name = $(this).find("name").text();
                      var group_id = $(this).find("id").text();
                      var group = {
                         id: group_id,
                         name: group_name
                      }
                      groups[counter] = group;
                      counter++;
                  });
                  callback.call(this,groups);
              }
         });
     }
}

TheObject.getArray(function(a) {
    // this code runs when the ajax call is complete
    alert(a);
});
David
Thanks a lot man!
ecu