Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

I'm having trouble using an array within javascript that I build from a jquery ajax call. I can create the array, and know it exists in the jquery function because I can log it in the browser console.I am not picking it up after the function runs though, as it comes back as undefined after the crews = loadCrews() line. Here's my main javascript file:

$(document).ready(function(){
     var crews = [];
    console.log("loading crews")
    crews = loadCrews();
   console.log(crews);

 $('#datetimepicker1').datetimepicker({
     pickTime: false
    });
 $('#datetimepicker2').datetimepicker({
       pickTime: false
    });

 $("#input_form").submit(function(){

     console.log(crews)
     var querystring = $(this).serialize();
             alert(querystring)
     return false;
         });

});

function loadCrews(){
 $.getJSON( "url", function( data ) {
    var items = [];
  $.each( data, function( key, val ) {
    items.push(val.UNID);
        })
     console.log(items)
    return items
 });   

};

Update 1: I've employed callbacks on the ajax request and it is starting to make sense. Thanks for all the good reads. Unfortunately I still can't pass back my array, even after using the .done callback.

Here's the updated code:

$(document).ready(function(){

        loadCrews();

 $('#datetimepicker1').datetimepicker({
     pickTime: false
    });
 $('#datetimepicker2').datetimepicker({
       pickTime: false
    });

 $("#input_form").submit(function(){
     var querystring = $(this).serialize();
             alert(querystring)

     return false;
         });

});

function loadCrews(){
    var url =  //url of service for all crew names
$.getJSON( url)
  .done(function( data ) {
      var crews = [];
    $.each( data, function( key, val ) {
        crews.push(val.UNID);
        })
    console.log(crews)
    return crews

  })
  .fail(function( jqxhr, textStatus, error ) {
    var err = textStatus + ", " + error;
    console.log( "Request Failed: " + err );
});
};
share|improve this question
2  
read this stackoverflow.com/questions/14220321/… –  Misters Feb 6 at 22:19
add comment

marked as duplicate by p.s.w.g, Quentin, Kevin B, Felix Kling, rene Feb 18 at 21:08

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers

Ajax calls are asynchronous, which means the javascript will continue to execute and not wait for the ajax call to finish first. That means that the crews variable is assigned to a value that doesn't exist yet (as the ajax call isn't finished when the variable is assigned).

You can either use a promise to assign the value when the ajax call is finished, or just assign the value to the crews variable inside the ajax success callback

share|improve this answer
add comment

You are returning items from inside $.getJSON thus nothing is being returned by loadCrews thus crews = nothing = 'undefined'. to fix this you can move the declaration var item = [] outside of the getJSON function. Goodluck!

share|improve this answer
    
@Pierre is also right, you need to wait for the ajax call to be completed before items will be ready to return. –  clancer Feb 6 at 22:26
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.