Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Provo, UT
    Posts
    142
    Thanks
    0
    Thanked 0 Times in 0 Posts

    AJAX returning response to a function.

    Here is a question that I have that you all may have an answer but I just can't figure it out. I have just worked around it but I am finally tired of that and want to see if there is a real answer out there.

    Here it is.

    I have a function that calls a function that sends an AJAX request to the server. When the response comes back, I want it returned to the original function so that I can handle the response.

    Here is some sudo codish stuff

    Code:
    function soSomething(){
    
    
    //do stuff
    
    
    response = doAJAXCall();
    
    alert(response);//alerts "UNDEFINED"
    
    }
    
    function doAJAXCall(){
    req = getXMLHTTPRequest();//makes the proper http request
    	
    	req.onreadystatechange = function () {
    		if (req.readyState == 4 ) {
    			returnXML = req.responseXML;
    			return returnXML;
    		}
    	}
    		
    	req.open("POST", 'some.php.file', true); 
    	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    	req.send(null);
    }
    the alert(response) line gives me an undefined. But if I alert returnXML instead of the return returnXML in the doAJAXCall function then I get a xml object.

    I always seem to be wanting to call an AJAX call and get the response but I can't do it. I have hacked through this by calling the same function that called the ajax function when the response comes in but that seems sloppy.

    I thought it was a problem with the line req.open("POST", 'some.php', true); so I changed it to req.open("POST", 'some.php', false); and it worked exactly the same.

    I am using firefox (I haven't tested in IE yet) and I am not sure if that makes a difference.

    Thank you in advance for any suggestions that you may have.
    I try to convince 'em that I am computer geek, but I just can't do it. Why? Oh why?

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    That is not going to happen. You are not dealing with a normal inline executing funciton you always have been working with.

    You owuld have to do an synchronous request with the XHR object which defeats the whole purpose of using it in my eyes.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Provo, UT
    Posts
    142
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I see. So I guess either you don't have this problem or you have just learned to deal with it in a different way.

    What do you do?
    I try to convince 'em that I am computer geek, but I just can't do it. Why? Oh why?

  • #4
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    I use a call back function and pass that when I make the ajax call. It uses eval, which is generally not good practice, but I find it's usefull in this case.

    Code:
    function getReqObjPost(url,params,func)
    {
    	if (window.XMLHttpRequest)
      	{
     		xmlhttp=new XMLHttpRequest();
     	}
     	else if (window.ActiveXObject)
        {
        	if(new ActiveXObject("Microsoft.XMLHTTP"))
        	{
        		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        	}
        	else
        	{
        		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
        	}
        }
      	
      	xmlhttp.onreadystatechange=function()
      	{
      		if(xmlhttp.readyState==4)
    		{	
    			if(xmlhttp.status==200)
    			{
    				var req_str=xmlhttp.responseText;
    				var req=xmlhttp.responseXML;
    				eval(func);
    			}
    			else
    			{
    				noData(xmlhttp.status);	
    			}
    		}
      	}
    	var now=new Date();
    	params=params + "&c_date"+now.getSeconds()+"=" + now;
    	xmlhttp.open("POST",url,true);
    	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    	xmlhttp.send(params);
    }
    Basscyst
    Helping to build a bigger box. - Adam Matthews


  •  

    Posting Permissions

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