Answer the question
In order to leave comments, you need to log in
Can't get data outside of success function of ajax request?
I send a request to the server, an array is returned. Within the success function, the array is available, beyond it - no longer.
Here is the code:
var arrayElem;
$.ajax({
type: 'GET',
url: '../getElemArray/',
data: {'name': elem},
dataType: 'json',
success: function(data) {
arrayElem = data.arrayElem;
alert(arrayElem); // Выводит: 1,2,3
}});
alert(arrayElem); // Выводит: undefined
Answer the question
In order to leave comments, you need to log in
So you have an asynchronous request. First, an alert will be displayed that is outside the function, and then when the request passes. will display an alert from success. Either put async: false in the ajax request parameters, or do all the processing in success
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question