|
From: RossGK on 25 Jun 2008 14:44 I'm a bit new to javascript - as will be obvious below. I'm using an XMLHttpRequest to get a bit of data from server (django), and it works nicely for single events. But my eventual outcome needs to be a series of data transmissions. I figured requesting data over and over until the data is some value that triggers the stop would be an interesting first attempt. To move forward, I wrapped my initial single data request in a for-loop to see if I could do multiple calls. Here's my javascript stuff... for (i=0;i<=5;i++) { xmlHttp.onreadystatechange=function() { if (xmlHttp.readyState==4) { document.myForm.myvar.value=xmlHttp.responseText; } } xmlHttp.open("GET","/ajax_data",true); xmlHttp.send(null); //alert("got here") dopause(500); //wait 500ms } What I've noticed is that with that alert line commented out, nothing seems to happen -ie I don't see the data receive. I expected to see it flash 5 times (the server sends a random number) When I put the alert in, the alert does in fact pop up, and the data gets displayed. Does the alert message cause a window refresh that I need to emulate when the alert is not there. What do I need to do to make it cycle through the get 5 times and actually show me what comes up, WITHOUT using an alert pop!? Thanks for any suggestions
From: after9 on 25 Jun 2008 15:27 On Jun 25, 2:44 pm, RossGK <ros...(a)gmail.com> wrote: > I'm a bit new to javascript - as will be obvious below. > > I'm using an XMLHttpRequest to get a bit of data from server (django), > and it works nicely for single events. But my eventual outcome needs > to be a series of data transmissions. I figured requesting data over > and over until the data is some value that triggers the stop would be > an interesting first attempt. To move forward, I wrapped my initial > single data request in a for-loop to see if I could do multiple calls. > > Here's my javascript stuff... > > for (i=0;i<=5;i++) { > xmlHttp.onreadystatechange=function() > > { if (xmlHttp.readyState==4) > { document.myForm.myvar.value=xmlHttp.responseText; > } > } > xmlHttp.open("GET","/ajax_data",true); > xmlHttp.send(null); > > //alert("got here") > dopause(500); //wait 500ms > } > > What I've noticed is that with that alert line commented out, nothing > seems to happen -ie I don't see the data receive. I expected to see > it flash 5 times (the server sends a random number) > > When I put the alert in, the alert does in fact pop up, and the data > gets displayed. > > Does the alert message cause a window refresh that I need to emulate > when the alert is not there. What do I need to do to make it cycle > through the get 5 times and actually show me what comes up, WITHOUT > using an alert pop!? > > Thanks for any suggestions Wish I could test this, but I don't have the resources. I'm assuming the myvar is a text field or text area in your form? Try appending the responsetext to this section - not just setting it (not as a permanent solution, mind you, but just to debug). It may be being overwritten with a null response.
From: RossGK on 25 Jun 2008 15:44 > Wish I could test this, but I don't have the resources. I'm assuming > the myvar is a text field or text area in your form? Yes, the text gets assigned to myvar in the form below... <form name="myForm"> Stuff: <input type="text" onkeyup="getServerData();" name="username" / > Response: <input type="text" name="myvar" /> </form> .... and as I say works nicely in a single event, or with the alert pop. > Try appending the responsetext to this section - not just setting it (not as a > permanent solution, mind you, but just to debug). It may be being > overwritten with a null response. Not sure what you mean... do you mean like some sort of print statement? (sorry my javascript weakness is showing)....
From: after9 on 25 Jun 2008 15:46 On Jun 25, 3:44 pm, RossGK <ros...(a)gmail.com> wrote: > > Wish I could test this, but I don't have the resources. I'm assuming > > the myvar is a text field or text area in your form? > > Yes, the text gets assigned to myvar in the form below... > > <form name="myForm"> > Stuff: <input type="text" onkeyup="getServerData();" name="username" / > > Response: <input type="text" name="myvar" /> > </form> > > ... and as I say works nicely in a single event, or with the alert > pop. > > > Try appending the responsetext to this section - not just setting it (not as a > > permanent solution, mind you, but just to debug). It may be being > > overwritten with a null response. > > Not sure what you mean... do you mean like some sort of print > statement? (sorry my javascript weakness is showing).... Try document.myForm.myvar.value+=xmlHttp.responseText; instead of document.myForm.myvar.value=xmlHttp.responseText;
From: RossGK on 25 Jun 2008 16:02 On Jun 25, 3:46 pm, after9 <ggama...(a)gmail.com> wrote: > > Try document.myForm.myvar.value+=xmlHttp.responseText; > > instead of > > document.myForm.myvar.value=xmlHttp.responseText; Same behaviour results. When I keep the alert box in there, I see the data get appended successively to the the previous data, and the form field fills up. Without the alert box, nothing shows up. Looking at my server log, interestingly I notice each time the alert pops up, the data is displayed and I dismiss the alert, the server shows that it has received a request and sent the data. Without the alert box, the server shows only one request (and nothing shows up on my page). So it seems the for-loop doesn't succeed in making multiple requests without the alert box. Will have to puzzle over that a bit longer...
|
Next
|
Last
Pages: 1 2 3 4 5 6 7 Prev: window.open problem Next: Regexp: Case-insensitive matching | N factorial |