Tuesday, 6 October 2009

AS3 Sandbox

Flash has some issues towards using foreign information, that is, you have an SWF file that has to access an external XML file, or other type of file. When this type of error occurs, you will get (if you have the Flash Player Debugger) a sandbox violation.

You have two ways to solve this... one that requires PHP or ASP, or any other script language knowledge, and the other requires that you have access to the source server (the one that has the information that you require).

This example will be to retrieve XML data, for example, a WebService.

1. By PHP, ASP or other script language (I will focus on PHP)
In order to do this you have to establish a "proxy" file, wich is, a file that will retrieve the data for you.
It works this way: In the same directory where the SWF file is you have to put a PHP (or ASP, or...) file that has a command to get the information. This is very simple.

In the SWF side:

 
function loadNews():void {
   var loader:Loader = new Loader();
   xml_NwsLoader.addEventListener(Event.COMPLETE, loadXMLNews, false, 0, true);
   xml_NwsLoader.load(new URLRequest("http://example.com/proxy.php"));
}

Notice that I will not call the XML file but a PHP one.

In the PHP side you must put a simple address call to get the data you want.

 
You can always look for a example and download files for Coldfusion, PHP, ASP and Java Servlet at this URL
http://kb2.adobe.com/cps/165/tn_16520.html

2. Using crossdomain
For this technique is supposed that you have access to the server where the XML file, or webservice is.

You must put a file at the server root called crossdomain.xml

You can see examples of this in detail in the Adobe site
http://kb2.adobe.com/cps/142/tn_14213.html


Cheers.