0

I want to get the response value from the get() method to use somewhere else in my code. Here is the code:

$.get('all-document-types', function( response ) {
    var data = [];
    $.each(response,function(index,value){
        data.push(value.document_type);
    });
    return data;
});

However, I am getting a undefined variable if I tried to access it outside of the callback function. So how can I get value?

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
newbieDev
  • 95
  • 3
  • 13
  • 3
    Define `var data = []` outside of the callback function. – j08691 Oct 23 '15 at 14:20
  • 1
    you could assign it to `window.whatever` BUT BUT BUT ! remember that the response happens asyncron and outside of this function `window.whatever` will not be assinged for some time. You probably want to check out `$.get(...).then().done().fail()` family of functions. – birdspider Oct 23 '15 at 14:22
  • Possible duplicate of [How to return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – Paul Roub Oct 23 '15 at 14:30

0 Answers0