How can I return the output of an XMLHttpRequest as a variable? The output doesn't get passed outside the XMLHttpRequest!
What am I doing wrong? I thought it was a problem related to the asynchronous nature of the request and I tried changing the flag to FALSE and even implemented a delay-promise mechanism to make sure the output would get returned only when processed, but still it does not get passed outside as a parameter...
var testAVal = "SomeStringToBeProcessed";
var outputStr = MyFunction(testAVal);
console.log("outputStr: ", outputStr); //ERROR! RETURNS undefined!
alert("outputStr: ", outputStr); //ERROR! RETURNS undefined!
function MyFunction(ValueToBeProcessed)
{
var ReqQ = new XMLHttpRequest();
ReqQ.onloadend = function()
{
if (this.readyState == 4 && this.status == 200)
{
return this.responseText;
}
};
ReqQ.open("GET", "myphpfunction.php?pV=" + ValueToBeProcessed, true);
ReqQ.send();
}