Having trouble with returning an object from the getMine
function in Javascript? Even though you try to print out the object, it keeps showing up as undefined
. How can you successfully return the obj
within this function?
function getMine() {
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState === 4) { // request is done
if (httpRequest.status === 200) { // success
var obj = JSON.parse(httpRequest.responseText)
return obj;
}
}
};
httpRequest.open('GET', "/getTest");
httpRequest.send();
}
var rs = getMine();
console.log("2", rs);