2009-06-05 61 views
0

我試圖在ActionScript中將URL參數添加到URL字符串中。目前我正在檢查現有的URL,看看它是否明確地有一個「?」確定是否有任何現有參數來確定我的參數分隔符是否應爲「?」或「&」。 ActionScript中是否有一個庫或實用方法可以簡化下面的代碼?將URL參數附加到動作腳本中的URL

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

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

回答

0

看看​​和URLLoader類。

+0

和URLRequest了。 – 2009-06-06 17:25:17

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' });