[英]Jquery autocomplete not showing suggestions
I am a newbie in web development and is trying to learn some jquery. 我是Web开发的新手,正在尝试学习一些jquery。 I was trying to populate some auto complete suggestions, but it is not working, this the script I used:
我试图填充一些自动完成建议,但是它不起作用,这是我使用的脚本:
$(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);
}
});
}
});
});
This is the response from the web service: 这是来自Web服务的响应:
[
{
"label": "asdfsadf",
"value": "1"
},
{
"label": "wrtwetr",
"value": "2"
}
]
When I send request from postman I am getting JSON response, but not from the web ui end and I am not getting any error. 当我从邮递员发送请求时,我收到JSON响应,但没有从Web ui端收到,并且没有收到任何错误。
1 $(document).ready(function() { 2 Setautocomplete(); 3 }); 1 $(document).ready(function(){2 Setautocomplete(); 3}); 4 5 6 Function Setautocomplete() { 7 $("#category").autocomplete({ 8 source: function(request, response) { 9 $.ajax({ 10 url: " http://localhost:8080/fun/getCat2 ", 11 headers: { 12 'Content-Type': 'application/x-www-. form-urlencoded' 13 }, 14 data: { 15 query: request.term 16 }, 17 type: "POST", 18 contentType: "application/json; charset=utf-8", 19 error: function(XMLHttpRequest, textStatus, errorThrown) { 20 alert(textStatus); 21 }, 22 onSelect: function(suggestion) { 23 alert('You selected: ' + suggestion.value + ', ' + suggestion.label); 24 } 25 }); 26 } 27 });
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 }
28}
Sorry I'm posting from mobile.please remove the line numbers 抱歉,我是从手机发布的,请删除行号
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.