简体   繁体   中英

Jquery autocomplete not showing suggestions

I am a newbie in web development and is trying to learn some 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:

[
    {
        "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.

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 }); 28 }

Sorry I'm posting from mobile.please remove the line numbers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM