Как получить доступ к сервису REST в Actionscript 3? - PullRequest
0 голосов
/ 18 ноября 2011

Я написал службу API в 'http://localhost:3000/api/user/id', сделать GET, может получить ответ JSON.

Как получить доступ к этому сервису из actionscript3?

1 Ответ

2 голосов
/ 18 ноября 2011

Вы можете сделать это с помощью простого 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);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...