I want to send a request to the server, and return the response using AJAX. I know that a good method for this is using a callback, however I am curious why xHTTP.onreadystatechange = function(){ ... }
can't access _response
variable.
function __GET(url)
{
var _response;
var xHTTP = new XMLHttpRequest();
xHTTP.open("GET", url, true);
xHTTP.onreadystatechange = function(){
if (xHTTP.readyState == 4){
_response = JSON.parse(JSON.stringify(xHTTP.responseText));
}
};
xHTTP.send();
return _response;
}