hi all,
Im new to ajax… please bear with me… My code is as follows:
function checkempid(){
var status;
xmlHttp=GetXmlHttpObject();
var urlemp="postemployee";
urlemp=urlemp+"?employeeId="+document.PostAvailableBench.employeeId.value;
xmlHttp.onreadystatechange=stateChangedemp ;
xmlHttp.open("GET",urlemp,true);
xmlHttp.send(null);
var showdata;
function stateChangedemp(){
if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
showdata = xmlHttp.responseText;
alert(showdata);
if(showdata.trim()=="available"){
alert("inside if");
saveReportFinished(true);
}
else{
alert("inside else");
saveReportFinished(false);
}
}
}
function saveReportFinished(success) {
if (success == true)
{
status = true;
}
else
{
status = false;
}
}
function GetXmlHttpObject(){
var xmlHttp=null;
try{
xmlHttp=new XMLHttpRequest();
}
catch (e) {
try {
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e){
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
return status;
}
Im trying to set the ‘status’ which is a global variable inside ‘saveReportFinished’ function and trying to return the status by using ‘return status’… but it returns only the initial value and does not return the values in saveReportFinished function. I will be glad if someone could help me… Thank You in advance!
The problem with trying to do what you want is that Ajax requests don’t run in a synchronous pattern which your code actually shows, instead what you need to do is change true to false in the send() method which will eliminate the code to continue running before a request is received. Also defining functions within a function may have been good practice a while back but with strict ECMA standards now for JavaScript 1.7 you should declared any functions/closures as variable references which validate but run the same way, see the below which i updated to respect both of my above comments.
function checkempid() {
// Define the "GetXmlHttpObject" function that creates the XMLHttpRequest object
var GetXmlHttpObject = function() {
var xmlHttp;
try {
xmlHttp = new XMLHttpRequest();
}
catch (e) {
try {
xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
}
catch (e) {
xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
}
}
return xmlHttp;
};
// Define the "saveReportFinished" function
var saveReportFinished = function(success) {
status = (success) ? true : false;
};
// Define the "onreadystatechange" callback
var stateChangedemp = function() {
if (xmlHttp.readyState === 4 || xmlHttp.readyState === 'complete') {
showdata = xmlHttp.responseText;
alert(showdata);
if (showdata.trim() === 'available') {
alert('inside if');
saveReportFinished(true);
} else{
alert('inside else');
saveReportFinished(false);
}
}
};
// Setup some local variables
var xmlHttp = GetXmlHttpObject(),
urlemp = "postemployee",
urlemp = urlemp + "?employeeId=" + document.PostAvailableBench.employeeId.value,
status, showdata;
xmlHttp.onreadystatechange = stateChangedemp;
xmlHttp.open('GET', urlemp, false);
xmlHttp.send(null);
return status;
}