How to access REST service in Actionscript 3?

后端 未结 1 1337
难免孤独
难免孤独 2021-01-17 04:22

I have write a api service at \'http://localhost:3000/api/user/id\', do a GET, can get a JSON response.

How to access this service from actionscript3?

相关标签:
1条回答
  • 2021-01-17 04:52

    You can do this using a simple URLRequest:

    var loader:URLLoader = new URLLoader();
    loader.dataFormat = URLLoaderDataFormat.TEXT;
    loader.addEventListener(Event.COMPLETE, loaderCompleteHandler);
    loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
    loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
    loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
    
    try
    {
        loader.load(new URLRequest("http://localhost:3000/api/user/id"));
    }
    catch (error:Error)
    {
        trace("Error loading URL: " + error.message);
    }
    
    function loaderCompleteHandler(e:Event):void
    {
        // and here's your response (in your case the JSON)
        trace(e.target.data);
    }
    
    function httpStatusHandler(e:HTTPStatusEvent):void
    {
        trace("httpStatusHandler:" + e.status);
    }
    
    function securityErrorHandler(e:SecurityErrorEvent):void
    {
        trace("securityErrorHandler:" + e.text);
    }
    
    function ioErrorHandler(e:IOErrorEvent):void
    {
        trace("ioErrorHandler: " + e.text);
    }
    
    0 讨论(0)
提交回复
热议问题