I'm trying to return an object in the onreadtstatechange function when making an AJAX request. I assign the returned object to a variable, but when i try to access the variable, I get this error (after trying to use the description property of the object):
Uncaught TypeError: Cannot read property 'description' of undefined
Here is what I'm doing:
var myApp = function() {
var obj1 = function() {
return {
something:function() {
var xmlhttp;
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
return {
status:xmlhttp.status,
description:xmlhttp.responseText
}
}
}
xmlhttp.open("GET","http://something",true);
xmlhttp.send();
}
}
}();
var obj2 = function() {
return {
something2:function() {
var something = obj1.something();
console.log("Description: "+something.description + ", Status: "+something.status);
}
}
}();
}();
Any suggestions?