Javascript AJAX调用,jQuery使用重复键生成查询字符串
Apache Solr要求发送到其端点的GET参数之一是重复的名称:Javascript AJAX调用,jQuery使用重复键生成查询字符串,javascript,jquery,solr,Javascript,Jquery,Solr,Apache Solr要求发送到其端点的GET参数之一是重复的名称: facet.range=price&facet.range=age 此处的文档: 在jQuery中,如何将该查询字符串参数(facet.range)包含两次?我无法使用重复的关键点创建对象,但这是我需要执行的操作: context = { 'facet.range': 'price', 'facet.range': 'age', // This will be the only element in
facet.range=price&facet.range=age
此处的文档:
在jQuery中,如何将该查询字符串参数(facet.range
)包含两次?我无法使用重复的关键点创建对象,但这是我需要执行的操作:
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) {
...
}
});
您可以手动将参数添加到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) {
...
}
});
我认为唯一的解决方法是将数据“硬编码”为查询字符串参数,而不是传递数据
$.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) {
...
}
});
我不熟悉ApacheSolr,但我知道您可以重新创建URL来传递参数
$.ajax({
type: "get",
url: 'http://127.0.0.1:8983/solr/select?'+ "facet.range=price&facet.range=age",
success:function (data) {
...
}
});
jQuery在内部使用
$.param
序列化表单,因此您也可以这样做:
data = $.param(
{ name: 'facet.range', value: 'price' },
{ name: 'facet.range', value: 'age' }
)
在您的params对象中使用
'facet.range':['price',age']
,并在ajax调用中将traditional
设置为true,以强制执行参数的“传统”序列化,即foo=1&foo=2
而不是foo[]=1&foo[]=2
,您可以将查询字符串传递给数据
参数。这看起来是最简洁的答案。我不喜欢手动生成GET查询。谢谢