I am facing an issue with a simple ajax request that retrieves a json encoded array. The error message I keep receiving is that the response is not defined. I suspect I have misplaced the return statement in my function. Here is how the function looks:
//Browser Support Code
function ajaxFunction(url,data){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var response = ajaxRequest.responseText
}
}
queryString = "?dta="+data;
ajaxRequest.open("GET", url + queryString, true);
ajaxRequest.send(null);
return(response);
}
Here is how I am calling the function:
var result = ajaxFunction('call.php','1');
alert(result);
The response is being successfully retrieved as the console displays:
{"stage1":"550","stage2":"1500","stage3":"2000","total":"1"}
However, I am encountering the error: response is not defined
Any insights on how to resolve this issue?