I need to capture a returned value and store it in a variable within an event handler function.
Below is a sample code snippet demonstrating this:
var xhr = new XMLHttpRequest();
function getData(e) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
e = JSON.parse(xhr.response);
return e;
} else {
return alert("Error!!");
}
}
}
var data;
xhr.addEventListener("load", function () {getData(data);}, true); // store the result in the 'data' variable
console.log(data);