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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Sep 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Make script wait until request comes back?

    Hello,

    In a function, I would like to make an ajax request, which returns data from a database. Instead of the requested data to be sent to a callback function, I would like to do something like this:

    Code:
    var requestData = {{ ajax request goes here }};
    I would like the script to wait at that line until the data comes back from the server, and be able to work with the data right away. I think it has something to do with the request being (a)synchronous, but all of the scripts I have tried send the data to a callback function.

    thanks in advance...

  • #2
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    There's just plain no way. You can tell the script to wait, but there is no gaurantee that the request has come back yet. When first starting out with Ajax, this was one of the hardest things for me to handle without having to seemingly write the same code over and over each time I made a request, so in the end this is what I came up with.


    This creates the request object, and sends it to any call back function you wish. It also allows you to pass variables to the callback function to be utilized after the request comes back.

    Code:
    //*******************************************************
    // Sends an asyncronous xmlhttp request using post
    // Pass the URL, Querystring, and callback function
    //*******************************************************
    function getReqObjPost(url,params,func){
    	var xmlhttp=false;
    	if(window.XMLHttpRequest){
    		try{
    			xmlhttp = new XMLHttpRequest();
    		}catch(e){
    			xmlhttp = false;
    		}
    	}else if(window.ActiveXObject){
    		try{
    			xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
    		}catch(e){
    			try{
    				xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    			}catch(e){
    				xmlhttp = false;
    			}
    		}
    	}
    	if(xmlhttp){
    		xmlhttp.onreadystatechange=function(){
    			if(xmlhttp.readyState==4){	
    				if(xmlhttp.status==200){
    					var str=xmlhttp.responseText;
    					var xml=xmlhttp.responseXML;
    					func(xml,str);
    				}else{
    					handleErrFullPage(xmlhttp.responseText)
    				}
    			}
    		}
    		var now=new Date();
    		params=params + "&cdate"+now.getSeconds()+"=" + encodeURIComponent(now);
    		xmlhttp.open("POST",url,true);
    		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    		xmlhttp.send(params);
    	}
    }

    Here's an example of it's usage.

    Code:
    var x=1
    var z=2
    var y=3
    getReqObjPost("PageYouReqest.asp","apple=1&orange=2&berries=3",function(xml,str){yourCallBackFuntion(xml,str,x,y,z);});
    
    function yourCallBackFunction(xml,str,x,y,z){
    var xmlObj=xml //this is the responseXML
    var strHTML=str //this is the responseText
    var xval=x;  //should hold a value of 1
    var yval=y; //should hold a value of 3
    var zval=z; //should hold a value of 2
    }
    Hope that helps,
    Basscyst
    Last edited by Basscyst; 09-14-2007 at 08:06 PM.
    Helping to build a bigger box. - Adam Matthews

  • #3
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Basscyst is correct, so you have to change your mindset when doing AJAX.

    If a user clicks fast enough they can issue multiple calls that may not return in the same order as they were issued. One way to slow people down in to tell people that something is happening. People will look at the spinning hourglass or ball and process what they see. This will take .3 seconds which is often enough time for the call to get processed.

    This is how I open and close an image when I issue any Ajax call.


    document.getElementById('hourglass').innerHTML="<img src=\"/images/infinity.gif\" alt=\"click me\" width=\"40\" height=\"40\" border=\"0\">";

    xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
    {
    // call returned so close hourglass
    document.getElementById('hourglass').innerHTML="";

  • #4
    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
    Make the Ajax call synchronous. [thrid parameter in open is set to false]
    The script will wait until the request comes back to the server.

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

  • #5
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    Doh, I thought they were saying they wanted it do be asynchronous, reading comprehension lapsed.
    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
    •