SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Not able to set the global variable through call back function in ajax !

    hi all,
    Im new to ajax.. please bear with me.. My code is as follows:

    Code:
    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!

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,192
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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.

    Code JavaScript:
    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;
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •