LoadVars.sendAndLoad( ) Method

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 18.  ActionScript Language Reference
LoadVars.sendAndLoad( ) Method Flash 6

transfer variables to a server-side application and receive variables in return
loadVarsObject.sendAndLoad(url, targetObject) loadVarsObject.sendAndLoad(url, targetObject, method)

Arguments

url

A string specifying the absolute or relative location of the external script or application that receives the Flash variables.

targetObject

A reference to the LoadVars object that will receive the server's response.

method

An optional string specifying the HTTP method by which to send the properties of loadVarsObject to an external script either "GET" or "POST". If omitted, it defaults to "POST". Unlike the send( ) method, sendAndLoad( ) can use "POST" successfully in all versions of the Standalone Player. See Appendix F for details on support for GET and POST.

Returns

A Boolean indicating whether the method executed successfully. Note that because the sendAndLoad( ) operation is asynchronous, the return value indicates only whether the operation commenced successfully; it may still eventually fail, in which case onLoad( ) will be invoked with a success value of false. The sendAndLoad( ) method returns false if it is passed the incorrect number of arguments (must be two or three), if the targetObject parameter is not an object, or if the request is prohibited due to security restrictions.

Description

The sendAndLoad( ) method converts all properties of loadVarsObject (excluding the contentType and loaded properties) to a string of URL-encoded variables, which it then sends to url in an HTTP request, using the method specified by method. The script or application at url is expected to return a string of URL-encoded variables, which are downloaded into the Player and converted to targetObject properties, as described under LoadVars.load( ). The targetObject's onLoad( ) handler executes when the properties become available. All properties are typed as strings.

Though a single LoadVars object can be used to both send and receive variables, it's best to separate the two operations into two LoadVars objects, as shown in the following Example. This keeps outgoing and incoming data confined to its own object and prevents unwanted transmission of the receiving object's properties, such as the loadVarsObject.onLoad handler.

For security reasons, LoadVars.sendAndLoad( ) works only with hosts in the domain from which the movie was downloaded. For more information, see System.security.

Usage

In practice, the sendAndload( ) and load( ) methods may not trigger a download if the variables at url are cached by the browser hosting the Flash movie. See LoadVars.load( ) for techniques that force the browser to bypass its cache.

Example

The following code uses a LoadVars object to send variables to the script lookup.pl. The varReceiver object catches the response sent by lookup.pl:

// Create the LoadVars objects. One to send... varSender = new LoadVars(); // ...and one to receive. varReceiver = new LoadVars();     // Assign properties to the sender, which will be the variables sent to the server. varSender.name = "Bruce"; varSender.age = "13";     // Assign a callback function to the varReceiver's onLoad property. // The handler will process the variables when they arrive. varReceiver.onLoad = function () {   // We can access the loaded variables as properties of this, as in this.age.   // We use a for-in loop to list the properties of the varReceiver object.   // Note that the loaded and contentType properties are not enumerated.   for (p in this) {     // We don't want to list the onLoad property, so check if the      // current property is a string. If it is...     if (typeof this[p] =  = "string") {       // ...show its value in the Output window. All loaded        // variables are received as strings.       trace("The variable " + p + " has the value " + this[p]);     }   } }     // Before sending our variables, we'll display them // in the Output window. (Good for debugging) trace("Sending " + varSender.toString());     // Now send the variables to the server, and wait for a reply varSender.sendAndLoad("http://www.yourserver.com/cgi-bin/lookup.pl",                        varReceiver, "GET");

See Also

LoadVars.load( ), LoadVars.onLoad( ), LoadVars.send( ), LoadVars.toString( )



    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net