AS3: Sending Data using POST


/ Published in: ActionScript 3
Save to your folder(s)

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'].


Copy this code and paste it in your HTML
  1. var bg_mc:MovieClip = new MovieClip();
  2. bg_mc.graphics.beginFill(0xFF0000, 1);
  3. bg_mc.graphics.drawRect(0, 0, 100, 100);
  4. bg_mc.graphics.endFill();
  5. bg_mc.x = stage.stageWidth / 2 - bg_mc.width / 2;
  6. bg_mc.y = stage.stageHeight / 2 - bg_mc.height / 2 ;
  7. bg_mc.buttonMode = true;
  8. bg_mc.addEventListener(MouseEvent.MOUSE_DOWN, visitSite);
  9. addChild(bg_mc);
  10.  
  11. function visitSite(e:MouseEvent):void {
  12. var url:String = "http://api.flickr.com/services/rest/";
  13. var request:URLRequest = new URLRequest(url);
  14. var requestVars:URLVariables = new URLVariables();
  15. requestVars.api_key = "3c84c0ca7f9ae17842a370a3fbc90b63";
  16. requestVars.method = "flickr.test.echo";
  17. requestVars.format = "rest";
  18. requestVars.foo = "bar";
  19. requestVars.sessionTime = new Date().getTime();
  20. request.data = requestVars;
  21. request.method = URLRequestMethod.POST;
  22.  
  23. var urlLoader:URLLoader = new URLLoader();
  24. urlLoader = new URLLoader();
  25. urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
  26. urlLoader.addEventListener(Event.COMPLETE, loaderCompleteHandler, false, 0, true);
  27. urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler, false, 0, true);
  28. urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler, false, 0, true);
  29. urlLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true);
  30. for (var prop:String in requestVars) {
  31. //trace("Sent: " + prop + " is: " + requestVars[prop]);
  32. }
  33. try {
  34. urlLoader.load(request);
  35. } catch (e:Error) {
  36. trace(e);
  37. }
  38. }
  39. function loaderCompleteHandler(e:Event):void {
  40. var responseVars = URLVariables( e.target.data );
  41. trace( "responseVars: " + responseVars );
  42.  
  43. }
  44. function httpStatusHandler( e:HTTPStatusEvent ):void {
  45. //trace("httpStatusHandler:" + e);
  46. }
  47. function securityErrorHandler( e:SecurityErrorEvent ):void {
  48. trace("securityErrorHandler:" + e);
  49. }
  50. function ioErrorHandler( e:IOErrorEvent ):void {
  51. //trace("ORNLoader:ioErrorHandler: " + e);
  52. dispatchEvent( e );
  53. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.