Dealing with the nature of asynchronous XMLHTTPRequest in JavaScript can be tricky. I faced a challenge where I needed to return the responseText value but found it impossible due to this nature. To overcome this, I came up with a workaround by calling a function that pastes the responseText into a specified ID. However, the issue resurfaced when I needed to retrieve a JSON file and return its value. I attempted to inject responseText into a variable, but it didn't work. I also tried creating an "interface-like" file to handle this task, but to no avail. Are there any known workarounds for this problem?
function httpGetJson( file, type) {
self.httpState(true);
try{
var type = type || true;
var http = self.http();
http.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200){
returnJson(this.responseText);
}
}
http.open("GET", "json/" + file, type);
http.send();
}catch (err){
return log.error("http.httpGetJson", err.message);
}
self.httpState(false);
}
Check it out on codepen http://codepen.io/mecostav/pen/JNdovR