我正在尝试使用Solr和JQuery UI实现HTML自动完成表单。
Solr的服务器记录查询,Firebug确认我得到了一个json数组:
{"responseHeader":{"status":0,"QTime":3,"params":{"fl":"defaultCommonNameString","q":"defaultCommonName:angle","wt":"json"}},"response":{"numFound":42,"start":0,"docs":[{"defaultCommonNameString":["angle"]},{"defaultCommonNameString":["reporter un angle"]},{"defaultCommonNameString":["formule pour un angle"]},{"defaultCommonNameString":["angle droit"]},{"defaultCommonNameString":["angle au centre"]}, ...]}}}
但是这段代码没有显示响应:
<link rel="stylesheet" type="text/css" href="css/demos.css" />
<script type="text/javascript" src="jquery/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="jquery/jquery.ui.core.js"></script>
<script type="text/javascript" src="jquery/jquery.ui.widget.js"></script>
<script type="text/javascript" src="jquery/jquery.ui.position.js"></script>
<script type="text/javascript" src="jquery/jquery.ui.autocomplete.js"></script>
<script>
$(function() {
$("#autosearch").autocomplete({
source: function( request, response ) {
$.ajax({
url: "http://localhost:8080/solr/select",
data: {
q: "defaultCommonName:"+request.term,
wt:"json",
fl: "defaultCommonNameString",
},
dataType: "json",
type: "GET",
success: function( data ) {
data=parse();
function parse(){
var parsedQueries=[];
for(var i=0;i<data.response.docs.length;i++){
parsedQueries[i]=data.response.docs[i].Query;
}
return parsedQueries;
}
response($.map(data.response.docs, function( item ) {
return {
label: item,
value: item
};
}));
}
});
},
minLength: 1
});
});
</SCRIPT>
</head>
<body>
<label for="autosearch">Search Here : </label>
<input id="autosearch"/>
</body>
感谢您的帮助
答案 0 :(得分:6)
非常感谢D_K,这工作正常!!!
代码:
$(function() {
$("#autosearch").autocomplete({
source: function( request, response ) {
$.ajax({
url: "http://localhost:8080/solr/select",
data: {
q: "defaultCommonName:"+request.term,
fl: "defaultCommonNameString",
wt: 'json',
},
dataType: "jsonp",
jsonp: 'json.wrf',
success: function( data ) {
response($.map(data.response.docs, function( item ) {
return {
label: item.defaultCommonNameString,
value: item.defaultCommonNameString,
};
}));
}
});
},
minLength: 1
});
});
答案 1 :(得分:1)
我使用上面的代码工作正常,但服务器IP和端口是直接在页面源上打开有没有办法加密这些信息。