如何使用dojo在查询字符串的URL中传递参数?

时间:2015-08-21 16:04:55

标签: javascript dojo

我编写了一小段代码,用于在查询字符串中传递参数。此函数仅传递单个参数。但我想传递多个参数。所以任何人都可以帮助我传递如p,q和r等额外参数。

这是我的代码:

searchTabLoaded: function() {
      var tmpQS = this.getQueryString();
      if ( tmpQS.hasOwnProperty('p') ) {
          var tmpProgram = decodeURIComponent(tmpQS['p']);
          this.searchTab.doSearch({'program': tmpProgram}, true);
          return;
      }
}

以上代码是我在网址中传递参数http://localhost/abc/def/sessions/?p=Something%20Here

的时候

然后它将在选项卡中加载所需的结果。但我想传递多个参数,如

http://localhost/abc/def/sessions/?p=Something%20Here&q=Some%20More%20Stuff&r=More%20More%20Stuff

与我想要构建的上述URL一样,取决于我们在URL中传递的参数。

doSearch()函数:

doSearch: function(theQuery, tmpPublishWhenDone) {
      console.log("Query: " + dojo.toJson(theQuery));
      this.startSearch();
    // get values from known widgets
    var tmpROUTINE = "doSearch";
    //WJF - If we have multi values in the query, we need to go "complex"
    var tmpQuery = theQuery || {};

    var tmpResultsDesc = this.getQueryDescription(tmpQuery);

    if (this._containsArray(tmpQuery)){
        //--- We need a complex query
        tmpQuery = this.convertToComplexQuery(tmpQuery);
    }

    var sortAttributes = this.getCurrentSortAttributes();

    var tmpResultsTitle = 'Search Results';
    var tmpToRun = this.publishResults;
    if( tmpPublishWhenDone == false ){
        tmpToRun = this.setResults;
    }

    var ds = this.getDS();

    var keywordArgs = {
          query: tmpQuery,
          resultsTitle: tmpResultsTitle,
          resultsDesc: tmpResultsDesc,
          sort: sortAttributes,
          onComplete: dojo.hitch(this,tmpToRun),
          onError: function(error) {
            // handle error
            console.error("query failed: ", error.message);
          },
          queryOptions: {
            ignoreCase:true,
            deep: true
          }
    };
    this.lastQuery = keywordArgs;

    try {
        ds.fetch(keywordArgs);      
    } catch(ex){

        this.processError(tmpROUTINE, ex, "running fetch with args -> " + typeof keywordArgs);      
    }

  }

以下函数将获取查询字符串。

getQueryString : function () {
      //--- also adds lower version to check
      var query_string = {};
      var query = window.location.search.substring(1);
      var vars = query.split("&");
      for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        var tmpKey = '' + pair[0];
        var tmpKeyLower = tmpKey.toLowerCase();
            // If first entry with this name
        if (typeof query_string[pair[0]] === "undefined") {
          query_string[pair[0]] = pair[1];
            // If second entry with this name
        } else if (typeof query_string[pair[0]] === "string") {
          var arr = [ query_string[pair[0]], pair[1] ];
          query_string[pair[0]] = arr;
            // If third or later entry with this name
        } else {
          query_string[pair[0]].push(pair[1]);
        }
        if( tmpKey != tmpKeyLower ){
            query_string[tmpKeyLower] = query_string[tmpKey];
        }

      } 
        return query_string;
  }

有人可以帮我使用Dojo吗?

谢谢..

1 个答案:

答案 0 :(得分:0)

您可以使用dojo的objectToquery方法轻松地从Javascript对象创建查询参数。

参考: https://dojotoolkit.org/reference-guide/1.7/dojo/objectToQuery.html