Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2 XMLHttpRequests, one always shot down

Status
Not open for further replies.

deanhiller

Programmer
Sep 29, 2005
5
US
I have an app, that every second sends an XMLHttpReqeust object out. In the same app when someone starts typing, I send a different XMLHttpRequest. When I put a 2 second delay in the server code handling the second one, I never receive it back. In fact any time, I get the response from the first request while the second is outstanding, I never get the second response.

I have tried using a different callback method for each request, firstResponseCallback, secondResponseCallback. When I do this, secondResponseCallback frequently receives the first request's reponse instead of the second one. It seems you can only have one outstanding XMLHttpRequest out at a time. Is there any way around this?

Is there any way to do synchronization in javascript? How do I deal with this? Without the sleep in the server, this problem is intermittent and very annoying. The sleep makes it happen every time.
 
I am confused. I thought I was, but maybe not. My functions are below. I do not have req declared as a global variable....hmmmm...maybe it created a global variable because of the code below? Can you help me rewrite this code so it works?

function timerFunction() {
createReq()
if(req) {
req.onreadystatechange = timerCallback;
req.open("GET", url, true);
req.send("");
}
}
function requestFunction() {
createReq()
if(req) {
req.onreadystatechange = callback;
req.open("GET", url, true);
req.send("");
}
}

Here is my create function

function createReq() {
req = false;
// branch for native XMLHttpRequest object
if(window.XMLHttpRequest) {
try {
req = new XMLHttpRequest();
} catch(e) {
req = false;
}
// branch for IE/Windows ActiveX version
} else if(window.ActiveXObject) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
req = false;
}
}
}
}

thanks,
dean
 
I can't really test what you have, but using the code I've put below should solve your problem. I would use this way instead...
Code:
function getHTTPObject() {
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				xmlhttp = false;
			}
		}
	@else
		xmlhttp = false;
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	return xmlhttp;
}

[b]var http1 = http2 = null;[/b]

function timerFunction() {
    [b]http1 = getHTTPObject();[/b]
    if(http1) {
        http1.onreadystatechange = timerCallback;
        http1.open("GET", url, true);
        http1.send("");
    }
}

function requestFunction() {
    [b]http2 = getHTTPObject();[/b]
    if(http2) {
        http2.onreadystatechange = callback;
        http2.open("GET", url, true);
        http2.send("");
    }
}
Basically, I found trying to re-use the same request object at the same time as another request was in progress was problematic. I came up with this solution - but would be interested in anyone has something better.

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
sweet, thanks much. I actually tried doing that after my post as I realized that might work. It is all working now.
thanks,
dean
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top