views:

36

answers:

2

How can we get data from the server in an asynchronous way in Flash? I am looking for something like XHR in actionscript.

+1  A: 

There are many ways. You can send HTTP request to the server and set a callback. You can even use direct socket connections (in Flex at least).

John
Can you direct me to an example where an HTTP request / callback is used? How do we use the data returned? Can something like a JSON be returned which we can use directly as an object / array?
Crimson
+3  A: 

If you use the URLLoader class you can request data from a server side script.

Personally I use JSON to communicate between server and flash (handy if you want to call the same scripts from javascript). There is a great library for decoding / encoding JSON strings in flash: http://code.google.com/p/as3corelib/

package {
    import flash.events.Event;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import com.adobe.serialization.json.*;

    class Test {
        private var loader:URLLoader;

        public function Test() {
            var request:URLRequest = new URLRequest("/api/myscript.py");
            loader = new URLLoader();
            loader.addEventListener(Event.COMPLETE, onLoadData);

            try {
                loader.load(request);
            }catch (error:SecurityError) {
                trace("security error");
            }

        }

        private function onLoadData(e:Event) {
            var obj:Object = JSON.decode(e.target.data);
        }

    }

}
Les
This was helpful. Thanks :)
Crimson