0

I have an AJAX get request where I want to send some of the data it receives into a global variable to be used elsewhere, but for some reason I can't seem to be able to connect the private variable with the global? Thanks.

$.ajax({
url: "-jsonurl-",
    type: "GET",
    data: {},
    dataType: "json",
    success: function (results) {
        AjaxResult(results);
    }      
});  

function AjaxResult(results) {
Label = results;
console.log(Label)  // <------ displays correct JSON data   
};

var Label;
console.log(Label)  // <------ returns as undefined???
Whirlwind991
  • 485
  • 5
  • 17
  • You *just declared* the variable on the previous line. Without a value. Why do you expect it to have a value? – David Dec 07 '17 at 03:18
  • 1
    It will return undefined because the ajax call will take a non-zero time to run, till then `Label` variable will remain undefined. – Varun Dec 07 '17 at 03:20
  • If it is really a global variable it should have a value. So, I suggest review your code first. – Zwei James Dec 07 '17 at 03:20
  • you should use promise in this case –  Dec 07 '17 at 03:20

0 Answers0