How to use source: function()... and AJAX in JQuery UI autocomplete
Inside your AJAX callback you need to call the response
function; passing the array that contains items to display.
jQuery("input.suggest-user").autocomplete({ source: function (request, response) { jQuery.get("usernames.action", { query: request.term }, function (data) { // assuming data is a JavaScript array such as // ["one@abc.de", "onf@abc.de","ong@abc.de"] // and not a string response(data); }); }, minLength: 3});
If the response JSON does not match the formats accepted by jQuery UI autocomplete then you must transform the result inside the AJAX callback before passing it to the response callback. See this question and the accepted answer.
Try this code. You can use $.get
instead of $.ajax
$( "input.suggest-user" ).autocomplete({ source: function( request, response ) { $.ajax({ dataType: "json", type : 'Get', url: 'yourURL', success: function(data) { $('input.suggest-user').removeClass('ui-autocomplete-loading'); // hide loading image response( $.map( data, function(item) { // your operation on data })); }, error: function(data) { $('input.suggest-user').removeClass('ui-autocomplete-loading'); } }); }, minLength: 3, open: function() {}, close: function() {}, focus: function(event,ui) {}, select: function(event, ui) {}});
$("#id").autocomplete({ search: function () {}, source: function (request, response) { $.ajax( { url: , dataType: "json", data: { term: request.term, }, success: function (data) { response(data); } }); }, minLength: 2, select: function (event, ui) { var test = ui.item ? ui.item.id : 0; if (test > 0) { alert(test); } }});