| David Mercer 2005-01-25, 7:23 pm |
| I am trying to detect an errors while using ServerXMLHTTP asynchronously, but
it appears that readyState remains 1 when an error occurs, and the only way I
have found for detecting the error is to use waitForResponse, but that does
not seem to work asynchronously (which is contrary to its advertised
behavior). Does anyone have any advice?
Here's how to reproduce my problem:
1. First, I set up an ASP page that will purposely stall 10 seconds before
returing a response. Here is the source for it:
wait10.asp:
--------------------------------------------------------------------------------
<script runat="server" language="JScript">
var before = new Date;
var now;
for (now = new Date; now < before.valueOf() + 10e3;) now = new Date;
</script>
<%= before %> and <%= now %>
--------------------------------------------------------------------------------
2. Next I ran the following script:
testasynch.js
--------------------------------------------------------------------------------
var exception;
var http = new ActiveXObject("Msxml2.ServerXMLHTTP.4.0");
// First try a known good URL.
http.open("GET", "http://localhost/wait10.asp", true);
http.send();
WScript.Echo("GOOD SEND:\t" + new Date + "\tREADY-STATE:\t" +
http.readyState);
while (!http.waitForResponse(1))
{
// If waitForResponse works as advertised, we expect this to loop ~9x
WScript.Echo("WAITING:\t" + new Date + "\tREADY-STATE:\t" + http.readyState);
}
WScript.Echo("COMPLETE:\t" + new Date + "\tREADY-STATE:\t" + http.readyState);
// Now try a known bad URL.
http.open("GET", "http://localhost_bad/wait10.asp", true);
http.send();
WScript.Echo("GOOD SEND:\t" + new Date + "\tREADY-STATE:\t" +
http.readyState);
try
{
while (!http.waitForResponse(1))
{
WScript.Echo("WAITING:\t" + new Date + "\tREADY-STATE:\t" +
http.readyState);
}
}
catch (exception)
{
WScript.Echo(exception.description);
}
WScript.Echo("COMPLETE:\t" + new Date + "\tREADY-STATE:\t" + http.readyState);
--------------------------------------------------------------------------------
3. We would expect the "WAITING" line to output about 9x if waitForResponse
were only waiting one second. Instead, it appears to wait the full 10s, at
which time it returns true because the response has been received. With the
second test, with the bad URL, waitForResponse appears to be the only way to
detect the error, however, since readyState remains 1 and the other
properties throw the error, "The data necessary to complete this operation is
not yet available", and so this situation is indistinguishable from a
long-running request. Here's the output:
--------------------------------------------------------------------------------
GOOD SEND: Tue Jan 25 11:08:13 CST 2005 READY-STATE: 1
COMPLETE: Tue Jan 25 11:08:24 CST 2005 READY-STATE: 4
GOOD SEND: Tue Jan 25 11:08:24 CST 2005 READY-STATE: 1
The server name or address could not be resolved
COMPLETE: Tue Jan 25 11:08:26 CST 2005 READY-STATE: 1
--------------------------------------------------------------------------------
Thank-you in advance.
|