Reputation: 9538
How would I load a resource (like a JSON or XML document) from the web in ActionScript 3?
Thanks
Upvotes: 0
Views: 806
Reputation: 17217
you will need to have a cross-domain policy file to access data that is not hosted on your own domain.
You cannot load variables or XML data into a Flash movie from another domain. For example, a Flash movie loaded from http://www.yourserver.com/flashmovie.swf can access data residing at http://www.yourserver.com/data.txt. The text file is located within the same domain as the SWF.
However, an attempt to load data from http://www.NotMyServer.com/data.txt will fail and no error messages are displayed. The load action will cause a warning dialog to appear.
Note: This security feature does not affect Flash movies playing in stand-alone projectors.
source: Cross-domain policy for Flash movies
once you have that set up, you can access the XML file via a URLLoader like this:
var XMLData:XML;
var XMLLoader:URLLoader = new URLLoader();
XMLLoader.addEventListener(Event.COMPLETE, XMLCompleteEventHandler);
XMLLoader.load(new URLRequest("http://my.xml.file"));
function XMLCompleteEventHandler(evt:Event):void
{
evt.currentTarget.removeEventListener(Event.COMPLETE, XMLCompleteEventHandler);
XMLData = new XML(evt.currentTarget.data);
}
i'm not sure if JSON files also require a cross-domain policy file, but i assume so. in either case, you may be able to bypass this security check by employing some JavaScript + ExternalInterface routine. the cross-domain policy file is not required for AIR applications.
you can find a JSON parser in as3corelib
Upvotes: 1
Reputation: 9572
There are many ways to achieve this and the question has been answered a few times here in various ways.
Try searching for Flash Php communication.
To load an XML document , look for the URLLoader class. For JSON, you can look into passing variables via SWFObject, also have a look at Zend Amf Server.
Upvotes: 0