package com.yourmajesty.services { import com.yourmajesty.async.*; import flash.events.*; import flash.net.*; /** * Simple form submission using GET or POST and URL-encoded variables. * Send the keys/values in an Object. * Get back a token which will populate with the result. * Extend this class and override extractFunction() to interpret the results differently. * You may also extend this class adding public static constants for the URLs of your services, * a la event names in Event subclasses. * @author Roger Braunstein 2007 */ public class FormSubmitter { public function send(url:String, hash:Object, method:String = URLRequestMethod.POST):IAsynchronousToken { var vars:URLVariables = new URLVariables(); for (var key:String in hash) { vars[key] = hash[key]; } var request:URLRequest = new URLRequest(); request.url = url; request.method = method; request.data = vars; var loader:URLLoader = new URLLoader(); loader.dataFormat = URLLoaderDataFormat.VARIABLES; try { loader.load(request); } catch (error:Error) { var t:AsynchronousToken = new AsynchronousToken(this, error); return t; } return new AsynchronousToken(loader, null, Event.COMPLETE, [IOErrorEvent.IO_ERROR, SecurityErrorEvent.SECURITY_ERROR], extractFunction); } protected function extractFunction(event:Event):* { return (event.target as URLLoader).data; } } }