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 9150539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:37:28+00:00 2026-06-17T11:37:28+00:00

I have been trying to figure out how to iterate thru the following code.

  • 0

I have been trying to figure out how to iterate thru the following code.

var arr = []

$.ajax({

 type: 'POST',
   dataType: 'html', 
   url:   '/test/public/index/getid', 
   success: function(response) {
        response = $.parseJSON(response)
        $.each(response, function(index, value) {
             arr.push(value)

        });
   }

});

console.log(arr)

$.each(arr, function(index0, value0) {

console.log(‘INDEX0: ‘ + value0);

});

This will give me a [ ] on firebug that I can expand. it has then 0 and 1 and shows the ids

[ ]

0 1234343

1 2343223

2 414234

3 232342

But later on program I try to loop thru it with $.each and it won’t read it.

I am trying to have arr when printed thru console.log to look like this.

[“1234343”, “2343223”, “414234”, “232342”]

The values inside are id’s that are pushed on that first loop.

Any idea on how to do this? trying to get arr.length and use it as a testing condition.

RAW JSON RESPONSE:
[“1234343”, “2343223”, “414234”, “232342”]

  • 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-06-17T11:37:29+00:00Added an answer on June 17, 2026 at 11:37 am

    Ajax is asynchronous, do not attempt to access data from the ajax request outside of it’s success callback.

    $.ajax({
       type: 'POST',
       dataType: 'json', 
       url: '/test/public/index/getid', 
       success: function(arr) {
           console.log(arr);
       }
    });
    

    You can also do it this way:

    var request = $.ajax({
        type: 'POST',
        dataType: 'json', 
        url: '/test/public/index/getid'
    });
    request.done(function(arr){
        console.log(arr);
    });
    

    The second way would allow you to pass request around and use it wherever you need it by simply doing this:

    request.done(function(arr){
        // do something with arr
        console.log(arr)
    });
    

    Update for comments,

    If request 2 depends on request one, use this structure:

    var request1 = $.ajax({...});
    
    request1.done(function(){
        var request2  = $.ajax({...});
        request2.done(function(){
            // both are done, do stuff
        });
    });
    

    or if you want to send request 1 and 2 and then do something when both are done, you can do this:

    var request1 = $.ajax({...});
    var request2 = $.ajax({...});
    $.when(request1,request2).done(function(req1,req2) {
        // do stuff
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to figure out why I am getting this problem and
I have been trying to figure out how to read paragraph content which exists
I have been trying to figure out how to open a file from the
I have been trying to figure out a solution but nothing has really presented
I have been trying to figure out a way to tag several methods from
I have been trying to figure out why and how to use performSelector. I
I'm a bit new to js and have been trying to figure out how
I'm working on a web-accessible API and have been trying to figure out the
I have been trying to figure this out for quite a while, but what
I have been trying to figure this out for way to long tonight. I

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.