/    Sign up×
Community /Pin to ProfileBookmark

Server response delayed AJAX

Hi!

Im doing an app where i get some data from a mssql db
That data is used to fill some <selects> so the data is taken from an asp script that returns a string with the data.
The scripts returns de data correctly.
That string is saved in a javascript variable for splitting it and with the resulting array, fill the options in the selects.
The problem occurs i think in my XMLHTTPRequest code
The onchange of the first <select> triggers the request for the data, but the first response is not what i expected.
But when i trigger again the request, the response from the server is the one i expected in the first time i triggered the request.
If i trigger the request again, the data that the server returns is the one i needed in the event just before.
And this gets happening.
Here is my code:
this function is called on the onchange event of my select

[CODE]
var respuesta=””;
function switch_Select(sorigen,sdestino,param,tabla)
{
var i;
i=0;
var lorigen = sorigen.options[sorigen.selectedIndex].value;
var ldestino = document.getElementById(“s”+sdestino);
if(sdestino==”request”)
sdestino=”fullname”;
if(sdestino==”respons”)
sdestino=”name”;
origenajax(lorigen,sdestino,param,tabla);
ldestino.options.length=0;
ldestino.options[0]=new Option(“–Seleccione–“,0);
var arreglo=respuesta.split(“:”);
var arrlength= arreglo.length;
for(i=1;i<arrlength;i++)
ldestino.options[i]=new Option(arreglo[i],arreglo[i]);
}
[/CODE]

This is my XMLHTTPRequest code.

[CODE]
function origenajax(id,col,param,tabla)
{
XMLHttp=null;
try
{
XMLHttp=new ActiveXObject(“Msxml2.XMLHTTP”);
}
catch(e)
{
try
{
XMLHttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}
catch(e)
{
XMLHttp=new XMLHttpRequest();
}
}
var url=”sel.asp”;
url+=”?id=”+String(id)+”&t=”+col+”&p=”+param+”&ta=”+tabla+”&sid=”+Math.random();
XMLHttp.onreadystatechange=stateChanged;
XMLHttp.open(“GET”,url,true);
XMLHttp.send(null);
}
function stateChanged()
{
if(XMLHttp.readyState==4||XMLHttp.readyState==”complete”)
respuesta=XMLHttp.responseText;
}
[/CODE]

Thanks

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@A1ien51Sep 12.2006 — You should be filling it in the onreadystatechange on value = 4. The request is asynchronous so it calls it and executes the remaining step in your funciton. That is why you see the effect of everything being delayed by one.

Eric
Copy linkTweet thisAlerts:
@JuanoauthorSep 13.2006 — That was the problem indeed.

So i call the selects-filling function inside my condition of readystate==4 and that resolved the problem.

Thanks for your help!!
×

Success!

Help @Juano spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...