Vous pouvez utiliser l'utilitaire HttpService et tirer parti de sa capacité à prendre des paramètres via un objet. Les paramètres peuvent être envoyés en tant que paires de valeurs clés, et la classe gère le reste.
Voici un exemple d'une méthode utilitaire qui fait exactement ceci:
public static function sendViaHttpService(url:String,
format:String,
method:String,
onComplete:Function,
onFail:Function,
parameters:Object=null):void {
var http:HTTPService = new HTTPService();
http.url = url;
http.resultFormat = format;
http.method = method;
// create callback functions which remove themselves from the http service
// Don't want memory leaks
var pass:Function = function(event:ResultEvent):void {
onComplete(event);
http.removeEventListener(ResultEvent.RESULT, pass);
}
var fail:Function = function(event:FaultEvent):void {
onFail(event);
http.removeEventListener(FaultEvent.FAULT, fail);
}
http.addEventListener(ResultEvent.RESULT, pass);
http.addEventListener(FaultEvent.FAULT, fail);
// yeah, we're going to send this in with the date to prevent
// browser-caching...kludgey, but it works
if (parameters == null) {
parameters = new Object();
}
// always get new date so the URL is not cached
parameters.date = new Date().getTime();
http.send(parameters);
} //sendViaHttpService()
Les paramètres peuvent être passés dans cette fonction statique comme ceci:
var complete:Function = function(event:ResultEvent):void { /* your
callback here */ };
var fail:Function = function(event:FaultEvent):void { /* your
failure callback here */ };
var url:String = "<your URL here>";
sendViaHttpService(url, URLLoaderDataFormat.TEXT, URLRequestMethod.GET, complete, fail, { param1: 'value1', param2: 'value2' });
Et URLRequest aussi. –