The following
snippet creates a URLRequest and URLLoader object, which loads the
contents of a text file on the network:
var request = new air.URLRequest("http://www.example.com/data/params.txt");
var loader = new air.URLLoader();
loader.load(request);
By default,
if you do not define a request method, the runtime loads the content using
the HTTP GET method. If you want to send the data
using the POST method, you need to set the request.method property
to POST using the static constant URLRequestMethod.POST,
as the following code shows:
var request = new air.URLRequest("http://www.example.com/sendfeedback.cfm");
request.method = air.URLRequestMethod.POST;
The external
document, params.txt, that is loaded at run time contains the following
data:
monthNames=January,February,March,April,May,June,July,August,September,October,November,December&dayNames=Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday
The file
contains two parameters, monthNames and dayNames.
Each parameter contains a comma-separated list that is parsed as strings.
You can split this list into an array using the String.split() method.
Tip: Avoid using reserved words or language constructs as variable names in external data files, because doing so makes reading and debugging your code more difficult.Once
the data has loaded, the Event.COMPLETE event is
dispatched, and the contents of the external document are available
to use in the URLLoader’s data property, as the
following code shows:
function completeHandler(event)
{
var loader2 = event.target;
air.trace(loader2.data);
}
If the
remote document contains name-value pairs, you can parse the data
using the URLVariables class by passing in the contents of the loaded
file, as follows:
function completeHandler(event)
{
var loader2 = event.target;
var variables = new air.URLVariables(loader2.data);
air.trace(variables.dayNames);
}
Each
name-value pair from the external file is created as a property
in the URLVariables object. Each property within the variables object
in the previous code sample is treated as a string. If the value
of the name-value pair is a list of items, you can convert the string
into an array by calling the String.split() method,
as follows:
var dayNameArray = variables.dayNames.split(",");
Tip: If you are loading
numeric data from external text files, you need to convert the values
into numeric values by using a top-level function, such as
parseInt(),
parseFloat(),
and
Number(). Instead
of loading the contents of the remote file as a string and creating
a URLVariables object, you could instead set the URLLoader.dataFormat property
to one of the static properties found in the URLLoaderDataFormat
class. The three possible values for the URLLoader.dataFormat property
are as follows:
Value
|
Description
|
air.URLLoaderDataFormat.BINARY
|
The URLLoader.data property
contains binary data stored in a ByteArray object.
|
air.URLLoaderDataFormat.TEXT
|
The URLLoader.data property
contains text in a String object.
|
air.URLLoaderDataFormat.VARIABLES
|
The URLLoader.data property
contains URL-encoded variables stored in a URLVariables object.
|
The following
code demonstrates how setting the URLLoader.dataFormat property
to air.URLLoaderDataFormat.VARIABLES allows you
to automatically parse loaded data into a URLVariables object:
var request = new air.URLRequest("http://www.example.com/params.txt");
var variables = new air.URLLoader();
variables.dataFormat = air.URLLoaderDataFormat.VARIABLES;
variables.addEventListener(air.Event.COMPLETE, completeHandler);
try
{
variables.load(request);
}
catch (error)
{
air.trace("Unable to load URL: " + error);
}
function completeHandler(event)
{
var loader = event.target;
air.trace(loader.data.dayNames);
}
Note: The default value for URLLoader.dataFormat is air.URLLoaderDataFormat.TEXT.
As the
following example shows, loading XML from an external file is the
same as loading URLVariables. You can create a URLRequest instance
and a URLLoader instance and use them to download a remote XML document.
When the file has completely downloaded, the complete event
is dispatched and the trace() function outputs
the contents of the file to the command line.
var request = new air.URLRequest("http://www.example.com/data.xml");
var loader = new air.URLLoader();
loader.addEventListener(air.Event.COMPLETE, completeHandler);
loader.load(request);
function completeHandler(event)
{
var dataXML = event.target.data;
air.trace(dataXML);
}