Sep 10 2008

crossdomain scripting : flash

Tag: flashvish @ 5:48 pm

If you are using dynamic data loading in flash, you might want to get data from remote servers. Flash doesn’t allows to access data from another domain, unless a cross domain policy is present on that server. The filed is names crossdomain.xml and should be present in document root. Here is an example on how a sample crossdomain.xml file looks :

Code:
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy
  SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
  <allow-access-from domain="www.yoursite.com" />
  <allow-access-from domain="yoursite.com" />
</cross-domain-policy>

Flash first loads this file before making any request to that server. In case this request fails a Security Sandbox error is thrown and flash dosn’t connects the same server again unless the swf reloads.

If you are trying to load an external swf present in other domain (usually applicable in case of overlay plugins), you must add Security.allowDomain in as3 code, which enables scripting this swf from other domains.

Code:
import flash.system.Security;
Security.allowDomain("*");

More detailed info on how to handle security issues in flash here.

Tags: crossdomain, dynamic loading, flash

Aug 17 2008

Loading dynamic data in flash (as2 and as3)

Tag: flashvish @ 3:42 pm

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.

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

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

Apr 26 2008

No _lockroot in as3

Tag: flashvish @ 2:52 pm

In AS3 there is no _lockroot. With as2, _lockroot used to fix the root of the movieclip to its orignal root, instead of the root of the file it is used in. In as3 it works as if _lockroot property is always set to true, so _root will reference to orignal root of the file.

So if working with as2, make sure what _root is referencing to… i had this issue sometime back

Tags: as3, flash