最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AJAX call having jQuery-built query string with duplicate keys - Stack Overflow

programmeradmin6浏览0评论

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:

.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) {
        ...
    }
});

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/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) {
        ...
    }
});
Share Improve this question edited Jun 9, 2014 at 1:42 Brad 163k55 gold badges377 silver badges552 bronze badges asked Sep 21, 2012 at 14:22 Mark LMark L 13.5k4 gold badges29 silver badges42 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 14

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.

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

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) {
           ...
       }
   });

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) {
        ...
    }
});

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) {
        ...
    }
});
发布评论

评论列表(0)

  1. 暂无评论