AS2 → AS3: LoadVars AS3 Equivalent
ActionScript 3 has done away with the LoadVars class and when I was updating my ContactForm for AS3 I was trying to figure out how to mimic the sendAndLoad() method that LoadVars provided. I stumbled upon an article by Peter Elst which explained how to do this so I'm going to outline the differences here.
Let's take a look at the old AS2 code:
-
var msg:LoadVars = new LoadVars();
-
var msgSent:LoadVars = new LoadVars();
-
-
msg.var1 = "one";
-
msg.var2 = "two";
-
-
msgSent.onLoad = function($success:Boolean):Void
-
{
-
if ($success)
-
{
-
trace("Message sent.");
-
}
-
else
-
{
-
trace("Message failed.");
-
}
-
};
-
-
msg.sendAndLoad("http://www.reintroducing.com/script.php", msgSent);
And here is the AS3 equivalent:
-
var scriptRequest:URLRequest = new URLRequest("http://www.reintroducing.com/script.php");
-
var scriptLoader:URLLoader = new URLLoader();
-
var scriptVars:URLVariables = new URLVariables();
-
-
scriptLoader.addEventListener(Event.COMPLETE, handleLoadSuccessful);
-
scriptLoader.addEventListener(IOErrorEvent.IO_ERROR, handleLoadError);
-
-
scriptVars.var1 = "one";
-
scriptVars.var2 = "two";
-
-
scriptRequest.method = URLRequestMethod.POST;
-
scriptRequest.data = scriptVars;
-
-
scriptLoader.load(scriptRequest);
-
-
function handleLoadSuccessful($evt:Event):void
-
{
-
trace("Message sent.");
-
}
-
-
function handleLoadError($evt:IOErrorEvent):void
-
{
-
trace("Message failed.");
-
}
As you can see, there is a new URLVariables class that stores the information you want to pass to your script. You then pass that URLVariables instance to the URLRequest's data property. Essentially, what this does, is create a query string with all the variables appended to it. The above actually looks like this when it is sent to the server:
http://www.reintroducing.com/script.php?var1=one&var2=two
That is what the PHP script receives and then handles it on the script's end to do whatever it is that you are wanting to do. I also explicitly set the URLRequest's method to be POST by setting the URLRequestMethod.POST constant (for GET, you'd just set URLRequestMethod.GET).
If you found this post useful, please consider leaving a comment, subscribing to the feed, or making a small donation.
10 Comments
[...] that sometimes what used to be accomplished in few steps now takes more. One of these is the old AS2 LoadVars object. It was pretty straight forward, but now it requires multiple classes and methods to make work. [...]
Maciej,
Can you read query string parameters with this URLVariables class. For instance in your example:
scriptVars.var1 = "one";
scriptVars.var2 = "two";
Say in the URL there was a querystring parameter that already exists http://www.dkdkdkdkd.com?var1=one
Can I do var strQueryStringValue:String = scriptVars.var1 and it store the value "one" in the string variable?
Richie,
Unfortunately it's not as easy as that. There has to be a javascript call to get the URL and then parse the URL in Flash. I'm gonna work on making a class to do this in AS3.











thanks a lot! useful.