如何在Actionscript 3中访问REST服务?

时间:2011-11-18 09:48:03

标签: json actionscript-3 rest

我已经在' http:// localhost:3000 / api / user / id'中编写了api服务,执行GET,可以获得JSON响应。

如何从actionscript3访问此服务?

1 个答案:

答案 0 :(得分:2)

您可以使用简单的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);
}