User Guide
Working with external data 373
The URLLoader.load() method (and optionally the URLLoader class’s constructor) takes a
single parameter,
request, which is a URLRequest class object. A URLRequest object
contains all of the information for a single HTTP request, such as the target URL, request
method (
GET or POST), additional header information, and the MIME type (for example,
when you upload XML content).
For example, to upload an XML packet to a server-side script, you could use the following
ActionScript 3.0 code:
var secondsUTC:Number = new Date().time;
var dataXML:XML =
<login>
<time>{secondsUTC}</time>
<username>Ernie</username>
<password>guru</password>
</login>;
var request:URLRequest = new URLRequest("http://www.yourdomain.com/
login.cfm");
request.contentType = "text/xml";
request.data = dataXML.toXMLString();
request.method = URLRequestMethod.POST;
var loader:URLLoader = new URLLoader();
try
{
loader.load(request);
}
catch (error:ArgumentError)
{
trace("An ArgumentError has occurred.");
}
catch (error:SecurityError)
{
trace("A SecurityError has occurred.");
}
The previous snippet creates an XML instance named dataXml that contains an XML packet
to be sent to the server. Next, you set the URLRequest
contentType property to "text/xml"
and set the URLRequest
data property to the contents of the XML packet, which are
converted to a string by using the
XML.toXMLString() method. Finally, you create a new
URLLoader instance and send the request to the remote script by using the
URLLoader.load() method.
There are three ways in which you can specify parameters to pass in a URL request:
■ Within the URLVariables constructor.
■ Within the URLVariables.decode() method.
■ As specific properties within the URLVariables object itself.