![](/img/trans.png)
[英]JQuery autocomplete returning data array with label and value, but not showing labels as suggestions
[英]Jquery autocomplete not showing suggestions
我是Web開發的新手,正在嘗試學習一些jquery。 我試圖填充一些自動完成建議,但是它不起作用,這是我使用的腳本:
$(document).ready(function() {
$("#category").autocomplete({
source: function(request, response) {
$.ajax({
url: "http://localhost:8080/fun/getCat2",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: {query : request.term},
type: "POST",
contentType: "application/json; charset=utf-8",
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
},
onSelect: function (suggestion) {
alert('You selected: ' + suggestion.value + ', ' + suggestion.label);
}
});
}
});
});
這是來自Web服務的響應:
[
{
"label": "asdfsadf",
"value": "1"
},
{
"label": "wrtwetr",
"value": "2"
}
]
當我從郵遞員發送請求時,我收到JSON響應,但沒有從Web ui端收到,並且沒有收到任何錯誤。
1 $(document).ready(function(){2 Setautocomplete(); 3}); 4 5 6函數Setautocomplete(){7 $(“#category”)。autocomplete({8 source:function(request,response){9 $ .ajax({10 url:“ http:// localhost:8080 / fun / getCat2 “,11個標頭:{ 12'Content -Type':'application / x-www-。form-urlencoded'13},14個數據:{15 query:request.term 16},17 type:” POST“ ,18 contentType:“ application / json; charset = utf-8”,19錯誤:function(XMLHttpRequest,textStatus,errorThrown){20 alert(textStatus); 21},22 onSelect:function(suggestion){23 alert('You已選擇:'+ proposal.value +','+ proposal.label); 24} 25}); 26} 27}); 28}
抱歉,我是從手機發布的,請刪除行號
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.