About XML 659
The following example shows how you can use the XML class’s onHTTPStatus event handler
to check whether an XML file successfully downloaded from the server and what the status
code returned from the HTTP request was.
Checking HTTP status codes using the XML class:
1. Create a new Flash document and save it as xmlhttp.fla.
2. Add the following ActionScript to Frame 1 of the main Timeline:
var my_xml:XML = new XML();
my_xml.ignoreWhite = true;
my_xml.onHTTPStatus = function(httpStatus:Number) {
trace("HTTP status is: " + httpStatus);
};
my_xml.onLoad = function(success:Boolean) {
if (success) {
trace("XML successfully loaded");
// 0 (No error; parse was completed successfully.)
trace("XML status is: " + my_xml.status);
} else {
trace("unable to load XML");
}
};
my_xml.load("http://www.helpexamples.com/crossdomain.xml");
The previous code defines a new XML object with the variable name my_xml, defines two
event handlers (
onHTTPStatus and onLoad), and loads an external XML file. The onLoad
event handler checks to see whether the XML file was successfully loaded and if so sends a
message to the Output panel as well as traces the XML object’s status property. It is
important to remember that the
onHTTPStatus event listener returns the status code
returned from the web server, whereas the
XML.status property contains a numeric value
that indicates whether the XML object was able to be parsed successfully.
3. Select Control > Test Movie to test the Flash document.
TIP
The XML.onHTTPStatus event handler is new to Flash Player 8.
WARNING
Don’t confuse the HTTP httpStatus codes with the XML class’s status property. The
onHTTPStatus event handler returns the server’s status code from an HTTP request
and the
status property automatically sets and returns a numeric value that indicates
whether an XML document was successfully parsed into an XML object.
CAUTION
If a web server does not return a status code to the Flash Player, the number 0 is
returned to the
onHTTPStatus event handler.