开发者

Something like AJAX in Flash?

How can we get data from the server in an asynchronous way in Flash? I am looking for something like XH开发者_如何学JAVAR in actionscript.


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);
        }

    }

}


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜