...

Make script wait until request comes back?

kellybanman
09-14-2007, 07:40 PM
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:

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...

Basscyst
09-14-2007, 08:00 PM
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.


//*******************************************************
// 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.


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

jameslove
09-14-2007, 10:14 PM
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="";

A1ien51
09-15-2007, 01:05 PM
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

Basscyst
09-15-2007, 02:18 PM
Doh, I thought they were saying they wanted it do be asynchronous, reading comprehension lapsed.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum