simshaun has answered your immediate question but I suspect that you may also have questions around the asynchronous nature of the AJAX call i.e. arrayData
will be undefined
until the response is retruned from the server and the success function is called.
Whatever you want to do with arrayData
after the success function is called will need to be called from within the success function. For example
var arrayData;
$.ajax({
type: "..",
url: "..",
data: ..,
success:function(data){
arrayData = jQuery.parseJSON(data);
doSomethingElse();
}
});
function doSomethingElse() {
/* do something else here with arrayData */
arrayData.doSomething();
}
Now at this point, we can actually remove arrayData
altogether
$.ajax({
type: "..",
url: "..",
data: ..,
success:function(data){
doSomethingElse(jQuery.parseJSON(data));
}
});
function doSomethingElse(data) {
/* do something else here with the returned data */
data.doSomething();
}
In fact, we can go even further. All the anonymous function that is assigned to the success property is really doing is calling doSomethingElse
so we could get rid of that and just call doSomethingElse
directly
$.ajax({
type: "..",
url: "..",
data: ..,
success: doSomethingElse
});
function doSomethingElse(data) {
/* do something else here with the returned data */
var arrayData = jQuery.parseJSON(data);
}
Is this cleaner/clearer?