0

I want to change my global variable "nyText" to another value that I got from an AJAX request. I made a callback function, and I am able to successfully alert "nyText" from inside it. As soon as I move the alert(nyText) outside of the callback function, the value is undefined.

AJAX:

    $.ajax({
  type: 'GET',
  url: 'http://JUST_A_LINK_IGNORE_THIS',
  success: 

  function informationData(data) {

    $.each(JSON.parse(data), function(imgName,order){


        var path_image = order[0].path_image;
        imgName = order[0].path_image;
        dataReceiver(imgName);  


    });
  }

});

Other code:

 var nyText;
function dataReceiver(imgName){


nyText = imgName;
//alert(nyText);  would have worked


}
dataReceiver();
alert(nyText); //will not work

Thank you!

Jeramo
  • 43
  • 6
  • That's because the `alert` is executed before the updated value is assigned to the `nyText` variable. AJAX = **Asynchronous ....** – Tushar Apr 10 '16 at 14:07
  • Yes, I know. But I thought that a callback-function would be sufficent? Also, it won't work if I set async: false – Jeramo Apr 10 '16 at 14:08

0 Answers0