Amit Singh Bhardwaj Amit Singh Bhardwaj - 1 month ago 10
reST (reStructuredText) Question

autocomplete from rest api

I'm using autocomplete function for auto suggetion and I'm using the rest api url as a data source but it is suggesting the all label for example if I enter

I
so it should display only that label which contain
I
but it is displaying all the label even when I type any keyword it is showing the all label here is my input text

<input type="text" id="name" />
<input type="text" id="value" />


here is my java script and jquery

$(function(){
$("#name").autocomplete({
source:"http://localhost:8080/JsonRestfull/countries",
dataType: "json",
select: function (event, ui) {
$("#name").val(ui.item.label);
$("#value").val(ui.item.value);
return false;
}
});
});


and the
http://localhost:8080/JsonRestfull/countries
contain the following data

[{"label":"IND","value":"India"},{"label":"CHN","value":"China"},{"label":"NEP","value":"Nepal"},{"label":"BHU","value":"Bhutan"}]

Answer

I guess you need something like this:

  $(function(){
 $.getJSON("http://localhost:8080/JsonRestfull/countries", function(data) {
  $( "#name" ).autocomplete({
     source: data    
       });
    });
});

I hope, It will help.

Comments