I want to return an object from inside getMine
function in Javascript but when I try to print out the object, I get undefined
. How would you return the obj
in the following function?
function getMine() {
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState === 4) { // request is done
if (httpRequest.status === 200) { // successfully
var obj = JSON.parse(httpRequest.responseText)
return obj;
}
}
};
httpRequest.open('GET', "/getTest");
httpRequest.send();
}
var rs = getMine();
console.log("2", rs);