Mark L
Mark L

Reputation: 13485

AJAX call having jQuery-built query string with duplicate keys

Apache Solr asks that one of the GET parameters sent to it's endpoint is a name duplicated:

facet.range=price&facet.range=age

Documentation here:

http://wiki.apache.org/solr/SimpleFacetParameters#facet.range

In jQuery, how can I include that query string parameter (facet.range) twice? I cannot make an object with duplicate keys, but this is along the lines of what I need to do:

context = {
    'facet.range': 'price',
    'facet.range': 'age', // This will be the only element in this dictionary as the key names are the same
}

$.ajax({
    type: "get",
    url: 'http://127.0.0.1:8983/solr/select',
    dataType:"jsonp",
    contentTypeString: 'application/json',
    jsonp:"json.wrf",
    data: context,
    success:function (data) {
        ...
    }
});

Upvotes: 8

Views: 2366

Answers (5)

georg
georg

Reputation: 215059

Use 'facet.range': ['price', 'age'] in your params object and set traditional to true in the ajax call in order to enforce "traditional" serialization of parameters, that is foo=1&foo=2 instead of foo[]=1&foo[]=2.

Upvotes: 14

David Hedlund
David Hedlund

Reputation: 129832

jQuery internally uses $.param for serializing forms, so you would be able to do the same:

data = $.param(
    { name: 'facet.range', value: 'price' }, 
    { name: 'facet.range', value: 'age' }
)

Upvotes: 4

CR41G14
CR41G14

Reputation: 5594

I am unfamiliar with Apache Solr but I know you can just re-create the URL to pass the parameters

$.ajax({
    type: "get",
    url: 'http://127.0.0.1:8983/solr/select?'+ "facet.range=price&facet.range=age",
    success:function (data) {
        ...
    }
});

Upvotes: -1

Simon
Simon

Reputation: 2830

I think the only way around that is to "hard code" the data as query string parameters instead of passing data

$.ajax({
    type: "get",
    url: 'http://127.0.0.1:8983/solr/select?facet.range=price&facet.range=age',
    dataType:"jsonp",
    contentTypeString: 'application/json',
    jsonp:"json.wrf",
    data: null,
    success:function (data) {
        ...
    }
});

Upvotes: 0

Sapan Diwakar
Sapan Diwakar

Reputation: 10966

You can add the arguments manually to the url.

   $.ajax({
       type: "get",
       url: 'http://127.0.0.1:8983/solr/select?facet.range=price&facet.range=age', // Add other parameters in the url
       dataType:"jsonp",
       contentTypeString: 'application/json',
       jsonp:"json.wrf",
       success:function (data) {
           ...
       }
   });

Upvotes: 2

Related Questions