User Guide
374 Networking and Communication
When you define variables within the URLVariables constructor or within the
URLVariables.decode() method, you need to make sure that you URL-encode the
ampersand (
&) character because it has a special meaning and acts as a delimiter. For example,
when you pass an ampersand, you need to URL-encode the ampersand by changing it from
&
to
%26 because the ampersand acts as a delimiter for parameters.
Loading data from external documents
When you build dynamic applications with ActionScript 3.0, itβs a good idea to load data
from external files or from server-side scripts. This lets you build dynamic applications
without having to edit or recompile your ActionScript files. For example, if you build a βtip of
the dayβ application, you can write a server-side script that retrieves a random tip from a
database and saves it to a text file once a day. Then your ActionScript application can load the
contents of a static text file instead of querying the database each time.
The following snippet creates a URLRequest and URLLoader object, which loads the
contents of an external text file, params.txt:
var request:URLRequest = new URLRequest("params.txt");
var loader:URLLoader = new URLLoader();
loader.load(request);
You can simplify the previous snippet to the following:
var loader:URLLoader = new URLLoader(new URLRequest("params.txt"));
By default, if you do not define a request method, Flash Player 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:URLRequest = new URLRequest("sendfeedback.cfm");
request.method = 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,Octo
ber,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.