B
B
belka_vs_suslik2014-04-05 15:13:29
JavaScript
belka_vs_suslik, 2014-04-05 15:13:29

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

1 answer(s)
G
Graph, 2014-04-05
@belka_vs_suslik

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 question

Ask a Question

731 491 924 answers to any question