Aug 17 2008
Loading dynamic data in flash (as2 and as3)
In as2 you can use LoadVars or XML class (load, send, sendAndLoad) to load Dynamic data. I’ll just post a simple example, livedocs is a great source for tutorial – so no point rewriting it. Have a look at LoadVars, and XML
Here in this example request will go with parameter myParam1, and response comes in format &showResponse=myResponse&. You can use XML in a similar way to send and load reponse in XML format. Also you can add listeners (httpStatus, ioError, securityError) to handle error events.
myLdr = new LoadVars();
myLdr.myParam1= "requestParam1";
myLdr.sendAndLoad("somefile.jsp", myLdr, "POST");
myLdr.onLoad = textLoaded;
function textLoaded(success) {
if (success) {
storeResponse = myLdr.showResponse;
}
}
In As3, you have URLLoader instead to do the same job. See complete documentation here.
Here is a simple example on how you can use it.
import flash.events.Event;
import flash.net.*;
var myRequest:URLRequest = new URLRequest("http://www.myurl.com/somefile.jsp");
var myLoader:URLLoader = new URLLoader();
var myVariables:URLVariables = new URLVariables();
myVariables.myParam= "param1";
myRequest.method = URLRequestMethod.GET;
myRequest.data = myVariables;
function onLoaded(evt:Event):void {
trace("loaded data is: "+myLoader.data);
}
myLoader.addEventListener(Event.COMPLETE, onLoaded);
myLoader.load(myRequest);
Important: Make sure URL you are calling is exactly in the same domain. For example, a SWF file at www.mydomain.com can load data only from sources that are also at www.mydomain.com. To load data from a different domain, place a cross-domain policy file on the server hosting the SWF file.
Tags: as2, as3, dynamic loading, flash