Jquery自动完成没有显示建议

时间:2017-12-12 18:31:29

标签: javascript jquery jquery-ui-autocomplete

我是网络开发的新手,正在尝试学习一些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);
                    }
                });
            }
        });
    }); 

这是来自网络服务的回复:

[
    {
        "label": "asdfsadf",
        "value": "1"
    },
    {
        "label": "wrtwetr",
        "value": "2"
    }
]

当我从邮递员发送请求时,我收到JSON响应,但不是来自网络ui,我没有收到任何错误。

1 个答案:

答案 0 :(得分:-1)

1 $(document).ready(function(){ 2     Setautocomplete(); 3 }); 4 五 6 函数Setautocomplete(){ 7     $( “#类”)。自动完成({ 8         source:function(request,response){ 9             $就({ 10                 网址:“http://localhost:8080/fun/getCat2”, 11                 标题:{ 12                     'Content-Type':'application / x-www-。窗体-urlencoded” 13                 }, 14                 数据:{ 15                     查询:request.term 16                 }, 17                 类型:“POST”, 18                 contentType:“application / json; charset = utf-8”, 19                 error:function(XMLHttpRequest,textStatus,errorThrown){ 20                     警报(textStatus); 21                 }, 22                 onSelect:function(suggestion){ 23                     alert('你选择:'+ suggestion.value +','+ suggestion.label); 24                 } 25             }); 26         } 27     }); 28 }

抱歉,我是从手机发布的。请删除行号