0

I'm trying to add a URL parameter to an URL string in ActionScript. Currently I'm checking the existing URL to see if it explicitly has a "?" to determine if there are any existing parameters to determine if my parameter delimiter should be "?" or "&". Is there a library or utility method in ActionScript which could simplify the code below?

var existingParameter:Boolean = existingUrl.indexOf("?") != -1;
var urlDelimiter:String = (existingParameter) ? "&" : "?";

var urlParameter:String = urlDelimiter + "ParameterName=" + parameterValue;
var completeUrl:String = existingUrl + urlParameter;
4

2 回答 2

0

看看URLVariablesURLLoader类。

于 2009-06-05T23:29:37.490 回答
0

您可以使用HttpService实用程序并利用它通过对象接收参数的能力。参数可以作为键值对发送,其余的由类处理。

这是一个实用方法的示例,它正是这样做的:

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()

参数可以像这样传递到这个静态函数中:

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' });
于 2009-06-05T23:32:37.237 回答