AS3: Sending and Recieving Data using a GET Request


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

I needed a way to do a Database lookup using GET. This simple example sends a GET requests and shows how to collect a response that would look like this "success=true&path=xyz.flv"


Copy this code and paste it in your HTML
  1. var requestVars:URLVariables = new URLVariables();
  2. requestVars.object_name = "key1";
  3. requestVars.cache = new Date().getTime();
  4.  
  5. var request:URLRequest = new URLRequest();
  6. request.url = "http://localhost:3000/videos/find_path/";
  7. request.method = URLRequestMethod.GET;
  8. request.data = requestVars;
  9.  
  10. for (var prop:String in requestVars) {
  11. trace("Sent " + prop + " as: " + requestVars[prop]);
  12. }
  13.  
  14. var loader:URLLoader = new URLLoader();
  15. loader.dataFormat = URLLoaderDataFormat.TEXT;
  16. loader.addEventListener(Event.COMPLETE, loaderCompleteHandler);
  17. loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
  18. loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
  19. loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
  20.  
  21. try
  22. {
  23. loader.load(request);
  24. }
  25. catch (error:Error)
  26. {
  27. trace("Unable to load URL");
  28. }
  29.  
  30. function loaderCompleteHandler(e:Event):void
  31. {
  32. var variables:URLVariables = new URLVariables( e.target.data );
  33. if(variables.success)
  34. {
  35. trace(variables.path);
  36. }
  37. }
  38. function httpStatusHandler (e:Event):void
  39. {
  40. //trace("httpStatusHandler:" + e);
  41. }
  42. function securityErrorHandler (e:Event):void
  43. {
  44. trace("securityErrorHandler:" + e);
  45. }
  46. function ioErrorHandler(e:Event):void
  47. {
  48. trace("ioErrorHandler: " + e);
  49. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.