1

I am having a scope issue.

function boo() {
    var ajax5 = new XMLHttpRequest();
    ajax5.onreadystatechange = function() {
        if (ajax5.readyState == 4) {
            xxx5 = (ajax5.responseText);

            var obj = JSON.parse(xxx5);
            d2 = obj.myvariable;

            return d2;

        }
    };
    ajax5.open("GET", "URL TO SCRIPT (CORS HEADERS ENABLED)", true);
    ajax5.send(null);


};


var xxx = boo();

If replace return d2 with console.log(d2) I get the value in console.

With return d2; I get undefined.

I want xxx to hold the value of d2.

Cody Raspien
  • 1,753
  • 5
  • 26
  • 51

1 Answers1

1

your "return d2" is of "onreadystatechange" not of "boo" function. That why your xxx is undefined

Khoa
  • 2,632
  • 18
  • 13