简体   繁体   中英

Twitter Typeahead 0.9.3 returning json object

I'm attempting to return a json object in my typeahead rather than just a single query. The idea is to pass along additional fields in my autocomplete menus to help filter down the results. Example year, make, model, trim. the json object would contain the filteredBy field id and field value. Example when searching make, the json object will contain the filter field id "year" and value"2013" plus the current field id and it's value.

Currently this works below, but only returns a single value.

 return $field.typeahead({
    replace: (uri, query) { 
       return extendURL(uri, "t:input": query);
    }
 }

This is how I'm trying to do it.

<input id="year" value="2013"/>
<input id="make" filterBy="year" value="ford"/>
<input id="model" filterBy="ford" value="fusion"/>


 init = function(spec) {
        var $field = $("#" + spec.id),
                filterIdArray = [];

        if (typeof spec.filterId !== 'undefined') {
            filterIdArray = spec.filterId.split(',');
        }

 return $field.typeahead({
    replace: function(uri, query) {
       var params = {};
           params["t:jsonStringField"] = searchField(spec.id, query);
           params["t:jsonStringFilter"] = searchFilter(filterIdArray);

       var stringify = JSON.stringify(params);

       //I'm trying to pass back this stringified json object.  
       return extendURL(uri, stringify);
    }
  }

    //The current search input
    searchField = function(fieldId, query) {
        return {name: fieldId, value: query};
    };

    //Additional fields used to create where clause within searchField
    searchFilter = function(filterIdArray) {
        var array = [];

        //Field Id's and any fields being filtered by
        for (var i = 0; i < filterIdArray.length; i++) {
            var value = $("#" + filterIdArray[i]).val(),
                 name = filterIdArray[i];

            array.push({name: name, value: value});
        };

        return array;
    };

I end up getting the following error.

"NetworkError: 500 Internal Server Error - /TapDemo/sell/createlisting.year:autocomplete?category=aircraft&0={&1=%22&2=t&3=:&4=j&5=s&6=o&7=n&8=S&9=t&10=r&11=i&12=n&13=g&14=F&15=i&16=e&17=l&18=d&19=%22&20=:&21={&22=%22&23=n&24=a&25=m&26=e&27=%22&28=:&29=%22&30=y&31=e&32=a&33=r&34=%22&35=,&36=%22&37=v&38=a&39=l&40=u&41=e&42=%22&43=:&44=%22&45=5&46=%22&47=}&48=,&49=%22&50=j&51=s&52=o&53=n&54=S&55=t&56=r&57=i&58=n&59=g&60=F&61=i&62=l&63=t&64=e&65=r&66=%22&67=:&68=[&69=]&70=}"

Does anybody know what I'm doing wrong?

您需要做的是,将这些值作为json编码发送,并确保将数据解码为数组,这样您就可以处理这些值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM