0

i am trying to push returned data from $.post method into global array...Please help me how to return entire array

var retval =[];
function returnOptionList(optName){

  $.post('getListTypes',{"key":optName},function(data) {

    if (data.ListNames == optName){

        for(var i=0; i<data.Values.length;i++){ 

               retval.push(data.Values[i]);
        }

      } 

    });
   return retval;       
}
user1985943
  • 83
  • 1
  • 3
  • 9
  • 1
    possible duplicate of [How to return the response from an Ajax call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – SomeKittens Sep 23 '14 at 18:31

1 Answers1

0

Use callbacks to return data from an AJAX call, not return

function returnOptionList(optName, callback){
    $.post('getListTypes',{"key":optName},function(data) {

        if (data.ListNames == optName){

            for(var i=0; i<data.Values.length;i++){ 

                retval.push(data.Values[i]);
            }

        } 
        callback(retval)
    });       
}

And call:

returnOptionList(optName, data) {
    console.log(data); //your array
})
tymeJV
  • 103,943
  • 14
  • 161
  • 157