Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 610369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:40:03+00:00 2026-05-13T17:40:03+00:00

TheObject = { getArray: function(){ var groups = new Array; $.ajax({ type: "POST", url:

  • 0
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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T17:40:04+00:00Added an answer on May 13, 2026 at 5:40 pm

    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);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have an object: var array = []; var theobject = null; array.push({song:The
Get object 's array value in php $obj = new Basecamp($bcUrl, $bcApikey, 'X', 'simplexml');
function myFunc(theObject) { theObject = {make: Ford, model: Focus, year: 2006}; } var mycar
I would like to know if 'theObject' is an enum (of any enum type)
So I have this code: function theObject(){ this.someelement = $(#someelement); this.someotherelement = $(someotherelement); this.someelement.fadeOut(500,
can anyone help me on how to retrieve function parameter names? For example: var
In my moosified TheObject package, I try to do this: around 'new' => sub
I'm writing data into a new ByteArray and storing it into LocalStore var ba:ByteArray
I have a function that looks like this: public UpdateRecord(MyObjectModel TheObject, int TheUserID) {
I hope to check the type of an object, for NSString: [theObject isKindOfClass:[NSString class]]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.