Search code examples
actionscript-3apache-flex

GET request Flex / Actionscript 3


Struggling to find any documentation to do a simple GET request to a URL to return JSON in Actionscript / Flex.

Anyone know


Solution

  • Use the URLLoader API.

    var myJSON :Object; //listing as per "key:" with "value"
    var str_JSON: String = ""; //if you need text String not data Object
    
    var siteloader :URLLoader = new URLLoader( new URLRequest( your_URL ) );
    
    siteloader.addEventListener(Event.COMPLETE, whenSiteLoaded);
    
    function whenSiteLoaded( evt :Event ) :void 
    {
      myJSON = JSON.parse(evt.target.data); //get into Object
      str_JSON = evt.target.data; //get into String
      //... any other code
    }
    

    You should add to the above the listeners for error conditions (eg: a "file not found" situation)