Adobe FLEX 2-PROGRAMMING ACTIONSCRIPT 3.0 Manual page 378

Programming actionscript 3.0
Table of Contents

Advertisement

Communicating with external scripts
In addition to loading external data files, you can also use the URLVariables class to send
variables to a server-side script and process the server's response. This is useful, for example, if
you are programming a game and want to send the user's score to a server to calculate whether
it should be added to the high scores list, or even send a user's login information to a server for
validation. A server-side script can process the user name and password, validate it against a
database, and return confirmation of whether the user-supplied credentials are valid.
The following snippet creates a URLVariables object named
variable called
. Next, a URLRequest object is created that specifies the URL of the
name
server-side script to send the variables to. Then you set the
URLRequest object to send the variables as an HTTP
object to the URL request, you set the
URLVariables object created earlier. Finally, the URLLoader instance is created and the
method is invoked, which initiates the request.
URLLoader.load()
var variables:URLVariables = new URLVariables("name=Franklin");
var request:URLRequest = new URLRequest();
request.url = "http://www.[yourdomain].com/greeting.cfm";
request.method = URLRequestMethod.POST;
request.data = variables;
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.addEventListener(Event.COMPLETE, completeHandler);
try
{
loader.load(request);
}
catch (error:Error)
{
trace("Unable to load URL");
}
function completeHandler(event:Event):void
{
trace(event.target.data.welcomeMessage);
}
The following code contains the contents of the ColdFusion® greeting.cfm document used in
the previous example:
<cfif NOT IsDefined("Form.name") OR Len(Trim(Form.Name)) EQ 0>
<cfset Form.Name = "Stranger" />
</cfif>
<cfoutput>welcomeMessage=#UrlEncodedFormat("Welcome, " & Form.name)#
</cfoutput>
378
Networking and Communication
method
POST
property of the URLRequest object to the
data
which creates a new
variables,
property of the
request. To add the URLVariables

Advertisement

Table of Contents
loading

This manual is also suitable for:

Flex

Table of Contents