Handling Live Web Content in WSH - Part 2 - Handling Errors and Exceptions
(Page 4 of 6 )
That was simple enough, but now what? There has to be more to it. For instance, what if there is a server error while we are trying to connect? How would we know, and better yet, how could we handle that in our script so that our script wouldn’t fail?
In this section, I will be dealing with several more parts of the XMLHttpRequest object.
Properties:
object.ReadyState
object.Status
object.StatusText
Methods:
abort()
getAllResponseHeaders()
getRespondseHeader()
Events:
onReadyStateChange (not used)
In order to catch errors and respond to them, we first need ways of monitoring what’s going on. There are basically two ways of doing this: by monitoring the XMLHttpRequest object’s connection state and by monitoring the server's HTTP response codes. The ReadyState and Status properties do that for us respectively. The ReadyState property returns one of five possible integer values as listed below based on the connection state of the XMLHttpRequest object.
Value | State | Description |
0 | Uninitialized | Waiting for the Open method. |
1 | Loading | Waiting for the Send method. |
2 | Loaded | Send method called. Status and headers available. |
3 | Interactive | Some data received. You may use ResponseBody and ResponseText to see currently available data. |
4 | Complete | All data has been received. |
The Status and StatusText properties are used to monitor the server responses. Status will return a numerical HTTP code and Status Text will return its text equivalent. So examples might be “404” and “Not Found”, respectively.
With these properties, we can easily monitor our conditions on both the client side and the server side. Our first concern would be a connection error in the form of a timeout. If we can’t make a connection in a reasonable amount of time we should stop the attempt before it throws an error in our script.
Next: Implementing a Workaround >>
More Windows Scripting Articles
More By Nilpo/Developer Shed Staff Writer