How would I return loadFile.responseText
as the return value in xhr() function where loadFile.Open is set to true (async)... When I set the request as synchronous (false), the value is returned. I do understand why this happens but I am wondering if there is a way to accomplish this with asynchronous settings.
loadFile.open("GET", url, true); // no response
Whole function:
function xhr(url, async){
var data = '';
var loadFile = new XMLHttpRequest();
loadFile.overrideMimeType("application/json");
loadFile.open("GET", url, false); // when true, no value is returned (of course)
loadFile.onreadystatechange = function() {
if (loadFile.readyState === 4 && loadFile.status == "200")
{
data = JSON.parse( loadFile.responseText );
return data;
}
}
loadFile.send(null);
return loadFile.onreadystatechange();
}
I was looking for an answer to this and found this How do I return the response from an asynchronous call? and it is NOT a duplicate.
I don't mind entirely changing the direction if it is required. Can you help with an actual basic example or a solid suggestion of which way to go to accomplish that?