I am trying to receive data from an API that contains JSON data that includes rundate and a start number. I want to store the start number as a global variable (test) to use in other functions.
var test=0;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var myObj = JSON.parse(this.responseText);
// retrieve data from server
document.getElementById("rundate").innerHTML = myObj[0].rundate;
document.getElementById("startnumber").innerHTML = myObj[0].startnumber;
var test = parseInt(myObj[0].startnumber);
}
};
// send data to server
xhttp.open("GET", "https://digitalordersdata.cfappstpanp.ebiz.verizon.com/getdigitalOrdersData", true);
xhttp.send();
alert(test);
So as you can see from above I am able to store and display the number I received by using the IDs but I am unable to redefine the variable test and use the number in another function. The alert still has the original value of 0.
Thanks in advance for the help.