Ryan Smith
Ryan Smith

Reputation: 8324

Flash - AJAX call

I want to call a web page from Flash and use the data returned from it (either in plain text or XML). I see with the NetConnection you can connect to a web service, but I just want to call a plain old web page.

It seems like I managed to do this a while back, but for the life of me, I can't find the answer on the web. Does anyone know what the function / code is to call a web page in Flash and get the data back?

Thanks,

Upvotes: 7

Views: 6196

Answers (2)

Alex Jillard
Alex Jillard

Reputation: 2832

All you need to do is use a URLLoader.

var urlRequest:URLRequest= new URLRequest("http://example.com/page/");
_urlLoader = new URLLoader();
_urlLoader.addEventListener(Event.COMPLETE, onXMLDataLoaded, false, 0, true);
_urlLoader.load(urlRequest);


function onXMLDataLoaded(evt:Event):void {      
    var xml = new XML(_urlLoader.data);
}

Upvotes: 12

Branden Hall
Branden Hall

Reputation: 4468

Well, if you're using AS3 then you'll want to use the URLLoader class. One common mistake using URLLoader is that you need to pass it an instance of URLRequest like so:

var loader:URLoader = new URLLoader();
loader.load(new URLRequest("http://www.stackoverflow.com"));

Note that you use the URLRequest object to specify and GET/POST parameters you want to send so in that case you want to build the URLRequest separately rather than inline.

Upvotes: 4

Related Questions