ActionScript 3.0 – where’s my LoadVars?

I recently got a question about what the ActionScript 3.0 equivalent is of the LoadVars sendAndLoad method. It may look like a lot more typing (and it is) but the whole implementation is very clean by separating out the request, loader and variables which you can only benefit from in the long run.

import flash.events.Event;
import flash.net.*;

var myRequest:URLRequest = new URLRequest("http://www.myserver.com/myscript.php");
var myLoader:URLLoader = new URLLoader();
var myVariables:URLVariables = new URLVariables();

myVariables.firstProperty = "first";
myVariables.secondProperty = "second";

myRequest.method = URLRequestMethod.GET;
myRequest.data = myVariables;

function onLoaded(evt:Event):void {
  trace("here we get the data back: "+myLoader.data);
}

myLoader.addEventListener(Event.COMPLETE, onLoaded);
myLoader.load(myRequest);

What happens in the code above is we set up a URLRequest instance that points to the server side resource you want to target. Next an instance of the URLLoader class is instantiated and we assign an instance of the URLVariables class to its data property.

This URLVariables instance basically abstracts the name/value pairs you want to send to the server. Since we specified the method of our URLRequest instance to use GET calling the load request essentially is equivalent to calling the following URL in your browser:

http://www.myserver.com/myscript.php?firstProperty=first&secondProperty=second

Finally before calling this load method we add an event listener to the URLLoader instance and set up a function to listen for the data we get back from the server side script.

That’s all cool, but what if you want to send an XML file your server side script (some JSON for example) instead of name/value pairs? Well no big deal, you can simply leave out the URLVariables instance and set the contentType property of the URLRequest instance to “text/xml” and assign your XML directly to the data property.

import flash.events.Event;
import flash.net.*;

var myRequest:URLRequest = new URLRequest("http://www.myserver.com/myscript.php");
var myLoader:URLLoader = new URLLoader();

var myXML:XML = Hello World;

myRequest.contentType = "text/xml";
myRequest.data = myXML;

function onLoaded(evt:Event):void {
  trace("here we get the data back: "+myLoader.data);
}

myLoader.addEventListener(Event.COMPLETE, onLoaded);
myLoader.load(myRequest);

3 Comments

  • November 28, 2007 - 5:13 am | Permalink

    Hi Peter,
    Just thought I’d throw in: say you have name/value flashVars dynamically written in your embed html page code and want to pull it in:

    try {
    var keyStr:String;
    var valueStr:String;
    var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;
    for (keyStr in paramObj) {
    valueStr = String(paramObj[keyStr]);
    if (keyStr == “stringYouAreLookingFor”) {
    name = value;
    }
    }
    } catch (error:Error) {
    trace(“error in loading”);
    }

    I’m trying to get in the habit of using try/catch/finally as it makes debugging less painful :)

  • chris rockwell
    March 28, 2008 - 2:48 am | Permalink

    Thanks for this.. it is a BIG help. But… I am new to actionscript as can not figure something out. I want to get something back from the onLoaded function. How the heck do I get it? I show an object here.. but it could be a string or anything. I am guessing I need to return the object, but how do I do that with the onLoaded function?

    here is the code:

    function makeCall(functionName:String, userID:String, params:Object):Object {
    var jParams:String = JSON.encode(params);
    var serviceUrl:String = “http://” + ServiceHost + “/” + ServicePath + “/” + functionName;
    var myRequest:URLRequest = new URLRequest(serviceUrl);
    var myLoader:URLLoader = new URLLoader();
    var myVariables:URLVariables = new URLVariables();
    myVariables.user = userID;
    myVariables.data = jParams;
    myRequest.method = URLRequestMethod.GET;
    myRequest.data = myVariables;

    function onLoaded(evt:Event):Object {
    var oPayload:Object = JSON.decode(myLoader.data); // here is the object I would like to return!
    }
    myLoader.addEventListener(Event.COMPLETE, onLoaded);
    myLoader.load(myRequest);
    }

    var ObjectIwant:Object = makeCall(“service”, “email”, oCredentials);

    Any help would be awesome

  • neeraj
    April 25, 2008 - 2:57 pm | Permalink

    Hi Peter,
    Just thought I’d throw in: say you have name/value flashVars dynamically written in your embed html page code and want to pull it in:

    try {
    var keyStr:String;
    var valueStr:String;
    var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;
    for (keyStr in paramObj) {
    valueStr = String(paramObj[keyStr]);
    if (keyStr == “stringYouAreLookingFor”) {
    name = value;
    }
    }
    } catch (error:Error) {
    trace(”error in loading”);
    }

    I’m trying to get in the habit of using try/catch/finally as it makes debugging less painful :)
    ———————————————

    this above code in AS-3

    can u give me same code in AS-2 plz..

  • Comments are closed.

    .