Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a legitimate array from ajax & jquery call

I create a simple ajax call, which retrieves data from my database with json, something like:

$.ajax({
    type: "..",
    url: "..",
    data: ..,
    success:function(data){
        var arrayData = jQuery.parseJSON(data);
    }
});

Then I get an array to arrayData, but how can I use it out of the 'success function'?

like image 870
Luis Avatar asked Mar 13 '26 16:03

Luis


1 Answers

You have to declare the arrayData var before the ajax call.

var arrayData;

$.ajax({
    type: "..",
    url: "..",
    data: ..,
    success:function(data){
        arrayData = jQuery.parseJSON(data);
    }
});

I recommend you read some on variable scope in javascript.

  • What is the scope of variables in JavaScript?
  • https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Core_Language_Features#Variable_Scope
  • http://www.google.com/search?q=javascript+variable+scope
like image 62
simshaun Avatar answered Mar 15 '26 06:03

simshaun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!