/ Published in: ActionScript 3
This function also shows how to pass variables to a REST service. This could also be used to pass vars that PHP can later collect as $_POST['params'].
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
var bg_mc:MovieClip = new MovieClip(); bg_mc.graphics.beginFill(0xFF0000, 1); bg_mc.graphics.drawRect(0, 0, 100, 100); bg_mc.graphics.endFill(); bg_mc.x = stage.stageWidth / 2 - bg_mc.width / 2; bg_mc.y = stage.stageHeight / 2 - bg_mc.height / 2 ; bg_mc.buttonMode = true; bg_mc.addEventListener(MouseEvent.MOUSE_DOWN, visitSite); addChild(bg_mc); function visitSite(e:MouseEvent):void { var url:String = "http://api.flickr.com/services/rest/"; var request:URLRequest = new URLRequest(url); var requestVars:URLVariables = new URLVariables(); requestVars.api_key = "3c84c0ca7f9ae17842a370a3fbc90b63"; requestVars.method = "flickr.test.echo"; requestVars.format = "rest"; requestVars.foo = "bar"; requestVars.sessionTime = new Date().getTime(); request.data = requestVars; request.method = URLRequestMethod.POST; var urlLoader:URLLoader = new URLLoader(); urlLoader = new URLLoader(); urlLoader.dataFormat = URLLoaderDataFormat.TEXT; urlLoader.addEventListener(Event.COMPLETE, loaderCompleteHandler, false, 0, true); urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler, false, 0, true); urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler, false, 0, true); urlLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true); for (var prop:String in requestVars) { //trace("Sent: " + prop + " is: " + requestVars[prop]); } try { urlLoader.load(request); } catch (e:Error) { trace(e); } } function loaderCompleteHandler(e:Event):void { var responseVars = URLVariables( e.target.data ); trace( "responseVars: " + responseVars ); } function httpStatusHandler( e:HTTPStatusEvent ):void { //trace("httpStatusHandler:" + e); } function securityErrorHandler( e:SecurityErrorEvent ):void { trace("securityErrorHandler:" + e); } function ioErrorHandler( e:IOErrorEvent ):void { //trace("ORNLoader:ioErrorHandler: " + e); dispatchEvent( e ); }