Question

So I need to call http://website.com/pagestuff?var1=123&var2=abc and I need to capture the json data returned. I can't for the life of me figure out how to capture a random url's http response data.

var apiRequest:URLRequest = new URLRequest("http://lb.website.com/public_api/get_data?t_uuid=6e55c370-9a76-4e7e-b5d9-f6fee4034662");
apiRequest.data.toString(); 

Just won't execute. How do I programmatically make a http request with get params and capture the responding data. I know I'm just not looking in the right place.

Was it helpful?

Solution

Requests in Actionscript are always asynchronous, therefore you must listen for the Event.COMPLETE before data becomes available. The URLRequest also does not communicate with external resources by itself, you need to pass it to the URLLoader object. Here's an example:

private function loadData():void
{
  var request:URLRequest = new URLRequest("http://yourdomain/api/params");
  var loader:URLLoader = new URLLoader();
  loader.addEventListener(Event.COMPLETE, onDataLoaded);
  loader.load(request);
}

private function onDataLoaded(e:Event):void
{
  e.target.removeEventListener(Event.COMPLETE, onDataLoaded);
  var data:String = e.target.data.toString();
  trace(data);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top