Multiple AJAX requests from single page- how to???
Rforbes
Posts: 281
Heya folks,
I'm trying to sort out a problem and not getting very far. My goal is to update different sections of a web page at different times- via AJAX.
My code is pretty straight forward on the spinneret side (W5100)...
But I'm struggling with the javascript side of things.
My javascript is: (called via the <script src=test.js</script> section on the htm file.)
I keep getting a "req2 is not defined" error in firefox error console, and the web page doesn't update the values. On the spinneret side, I use PST to verify that the appropriate method is called, and it is indeed called. So I know xml data is being spat out, but there is something wrong with the way I'm trying to use AJAX here.
Any advice, ideas please? I'd sure appreciate it!
Thanks in advance!
Robert
I'm trying to sort out a problem and not getting very far. My goal is to update different sections of a web page at different times- via AJAX.
My code is pretty straight forward on the spinneret side (W5100)...
PRI Dispatcher(id) '' HTTP request handler ''Live Data Output if(strcomp(Request.GetPathNode(id, 0), string("First"))) Update_Time(id,0,46) return if(strcomp(Request.GetPathNode(id, 0), string("Second"))) Update_Data(id) return
But I'm struggling with the javascript side of things.
My javascript is: (called via the <script src=test.js</script> section on the htm file.)
function DataLoop(){ var req1 = getXmlHttpRequestObject(); setTimeout(function(){getData1('First')},2000); var req2 = getXmlHttpRequestObject(); setTimeout(function(){getData2('Second')},4000); } function getData1(resource){ // handle the case where a querystring is not detected var char = "&"; if(resource.indexOf("?", 0) == -1) { char = "?"; } if (req1.readyState == 4 || req1.readyState == 0) { req1.open("GET", resource + char + 'ms=' + new Date().getTime(), false); req1.onreadystatechange = handleResponse; req1.send(null); return false; } } function handleResponse() { if (req1.readyState == 4) { parseState(req1.responseXML); } } function parseState(xDoc){ if(xDoc == null) return // IO data starts here var target = document.getElementById("Date"); target.innerHTML = xDoc.getElementsByTagName("Date")[0].childNodes[0].nodeValue; var target = document.getElementById("Time"); target.innerHTML = xDoc.getElementsByTagName("Time")[0].childNodes[0].nodeValue; } function getData2(resource){ // handle the case where a querystring is not detected var char = "&"; if(resource.indexOf("?", 0) == -1) { char = "?"; } if (req2.readyState == 4 || req2.readyState == 0) { req2.open("GET", resource + char + 'ms=' + new Date().getTime(), false); req2.onreadystatechange = handleResponse; req2.send(null); return false; } } function handleResponse() { if (req2.readyState == 4) { parseState(req2.responseXML); } } function parseState(xDoc){ if(xDoc == null) return // IO data starts here var target = document.getElementById("Speed"); target.innerHTML = xDoc.getElementsByTagName("Speed")[0].childNodes[0].nodeValue; var target = document.getElementById("Pressure"); target.innerHTML = xDoc.getElementsByTagName("Pressure")[0].childNodes[0].nodeValue; } function getXmlHttpRequestObject() { if (window.XMLHttpRequest) { return new XMLHttpRequest(); } else if (window.ActiveXObject) { return new ActiveXObject("Microsoft.XMLHTTP"); } else { alert("Your Browser does not support AJAX- Please upgrade to latest version of Firefox."); } } setInterval (function(){DataLoop},6000);
I keep getting a "req2 is not defined" error in firefox error console, and the web page doesn't update the values. On the spinneret side, I use PST to verify that the appropriate method is called, and it is indeed called. So I know xml data is being spat out, but there is something wrong with the way I'm trying to use AJAX here.
Any advice, ideas please? I'd sure appreciate it!
Thanks in advance!
Robert
Comments
You can either give all definitions unique names, or fold the script for both requests into those functions.
Thanks very much! I will try some more tonight when I get a chance and see what I come up with.
Robert
So... that brings up another question. if I changed getXmlHttpRequestObject so that my req1 variable is defined via getXmlHttpRequestObject1 and my req2 variable is defined via getXmlHttpRequestObject2, and made the appropriate changes to handleResponse and parseState by making them 1 and 2- would I then be able to skip using the setTimeout function? I guess what I'm asking is- if I don't use the setTimeout, both req1 and req2 will fire at just about the same time.... so I'm not sure how the spinneret would respond to that? Will it just que up the second one and run when it's able? I hope my question makes sense. Sorry if it doesn't!
You can just setInterval for each, without the dataloop. Whether or not you set timeouts is up to you.
Robert
To help clarify my application, the reason I am trying to do multiple ajax thingies (that's the technical term, btw) is because in doing so, I can greatly reduce the RXTX_BUFFER and TEMP_BUFFER size, thereby giving me more room to play with fabulous, mind boggling, snazzy methods and such. Or at least I have more memory space to create such things one day! :cool:
As a minor update, I created this .js and it seems to be working pretty well. I'm trying to bone up on using a callback function (I think that's what I need, not certain) so that the various ajax calls will only fire when the previous one is proven to be complete. If you have some good advice I could really use it! (My wife told me to stop being such a geek... I don't need more of that advice! haha!)
Robert
Works in Safari (win & osx)
Not working in explorer & chrome .
LTech
In Windows IIS 6.5
IIS 7 is similar. When you right click the folder in Internet Information Services you can select Create Application, if memory serves...
BTW, post #10 and getios demos how to handle an array of values while making the XML light weight and reusable. This approach will save precious Prop Memory. I hope that concept was conveyed.
Robert
Was...
Updated To...
This will cause IE to pop up a warning when the file is opened and will generate an Access is Denied error if the link is clicked. Still can't run the demo from the file system but at least the error message is a bit more descriptive.
I'll leave that up to you. Let me know if you need help. Use the two files attached to #10 to build a simple testing rig. Verify functionality before implementing on the prop. It will save you a lot of time.