【问题标题】:Appending URL parameters to URLs in ActionScript将 URL 参数附加到 ActionScript 中的 URL
【发布时间】:2009-06-05 21:37:24
【问题描述】:

我正在尝试将 URL 参数添加到 ActionScript 中的 URL 字符串。目前我正在检查现有的 URL 以查看它是否明确包含“?”确定是否有任何现有参数来确定我的参数分隔符是否应该是“?”或者 ”&”。 ActionScript 中是否有可以简化以下代码的库或实用程序方法?

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

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

【问题讨论】:

    标签: apache-flex url actionscript delimiter


    【解决方案1】:

    看看URLVariablesURLLoader 类。

    【讨论】:

      【解决方案2】:

      您可以使用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' });
      

      【讨论】:

        猜你喜欢
        • 2019-07-09
        • 2015-04-22
        • 2013-12-25
        • 1970-01-01
        • 2012-12-19
        • 2023-03-28
        • 2012-07-04
        • 2015-12-26
        • 1970-01-01
        相关资源
        最近更新 更多