S
S
Sergey2021-04-29 22:02:36
JSON
Sergey, 2021-04-29 22:02:36

How to determine if JSON has already been loaded?

Made a JSON request. The data has arrived.
But I can't use them outside of $.get ! Why is the alert not displayed?

$(function(){

$.get('file.php',	obrabotka,'json');

function obrabotka(data){
  var arr = Object.keys(data).map((key) => [key, data[key]]	);
  };
alert("снаружи"+arr);

});

PS: I use jQuery

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Fedorov, 2021-04-30
@Shull

Getting JSON via ajax is an asynchronous operation. JavaScript, on the other hand, is synchronous, so the $.get and alert calls happen instantly one after the other, and only then the obrabotka function is called .
You can only access arr inside the obrabotka function .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question