Peter Elst

Flash Platform Consultant
  • Home
  • About me
  • Articles
  • Downloads
  • Contact me

ActionScript 3.0 - where’s my LoadVars?

28 11 2007

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);

« ActionScript 3.0 Design Patterns Skinning Components in Flash CS3 »



Actions

  • rss Comments rss
  • trackback Trackback

Informations

  • Date : 28 November 2007
  • Categories : Flash, Flex

3 responses to “ActionScript 3.0 - where’s my LoadVars?”

28 11 2007
Michael Kaufman (05:13:32) :

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 :)

28 03 2008
chris rockwell (02:48:28) :

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

25 04 2008
neeraj (14:57:43) :

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..

Leave a comment

You can use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>





Training Partners

multimediacollege

SkillsMatter

  • Categories

    • AIR RSS category feed
    • Ajax RSS category feed
    • CSS RSS category feed
    • Eclipse RSS category feed
    • Events RSS category feed
    • Flash RSS category feed
    • Flash Lite RSS category feed
    • Flex RSS category feed
    • Gadgets RSS category feed
    • General RSS category feed
    • JSFL RSS category feed
    • Mac RSS category feed
    • Open Source RSS category feed
    • PHP RSS category feed
    • Podcasts RSS category feed
    • Publications RSS category feed
    • Rants RSS category feed
    • Reviews RSS category feed
    • Thought of the Day RSS category feed
    • Training RSS category feed
    • Video RSS category feed
  • Resources

    • Serge Jespers
    • Matt Voerman
    • Peldi Guilizzoni
    • Stefan Richter
    • Simon Barber
    • Veronique Brossier
    • Sas Jacobs
    • Rich Tretola
    • Peter Hall
    • Josh Tynjala
    • Brajeshwar Oinam
    • Darron Schall
    • Stephen Downs
    • Edwin van Rijkom
    • Matthew David
    • Marco Casario
    • Patrick Mineault
    • Branden Hall
    • Keith Peters
    • Burak Kalayci

     
     

    Adobe Community Expert

    See my profile on LinkedIn



    Harz Ferienwohnung Suchmaschinenoptimierung Geschenkideen Harz Ferienwohnung Pagerank Webkatalog Webhosting