This seems to be a black hole: After an hour of searching the jQuery UI website, Stack Overflow, and googling, I've yet to find the most basic information of how to write the server side of the AutoComplete.
What parameter is passed to the server and what should the JSON response look like?
I must be missing something, because how did everyone else learn how to do this? Sites only seem to discuss the client-side JavaScript code and never the protocol or server-side examples.
I need enough to get the simplest remote example working.
You need to pass
request.term
to your server-side code (from the documentation):Basically, in your
autocomplete
code, you'll have something like this:The
autocomplete
widget expects an array of JSON objects withlabel
andvalue
properties (although if you just specifyvalue
, it will be used as the label). So in the simplest case, you can just return data that looks like this:If you need something more complicated, you can use the
success
argument of the$.ajax
function to normalize the data you get back before the autocomplete gets it:This code is taken from the example here (This is a good example overall of ajax + autocomplete works in a more complex scenario).
Basically, what's going is that upon a successful ajax request, the data received is being normalized (using
$.map
) to what the autocomplete widget expects.Hope that helps.
The following Autocomplete is from https://jqueryui.com/autocomplete/#remote-jsonp
A demo link: https://jqueryui.com/resources/demos/autocomplete/remote-jsonp.html
Here is source code:
The following code is working for me. This needs json encoded data to work. Once we get data, it modifies it according to jQuery autocomplete format and also enables selection
You are not required to tweak the server side script in order to use jQuery UI autocomplete. You can specify a JavaScript function as the
source
to create custom requests (e.g. use POST or GET, use query string parameters that the serever side script expects) and handle arbitrary responses (e.g. handle XML responses).Having said that, when you use a string as the
source
parameter, then:Regarding "The data itself can be in the same format as the local data described above", the following JSON (or JSONP) formats will work:
For the arrays of objects, you are free to specify additional properties besides label and/or
value
. All properties will be available inside callbacks.Both answers so far are complex and misleading, a key understanding to jQuery UI Auto Complete is the success anonymous function, you have leverage/control of the format of your server side JSON response because of the success callback of AutoComplete. The label,value format is a good one to follow but you can define any JSON format you desire, the key is how you define your success function:
MVC Controller:
Here we see a very standard auto complete bind with an ASP.NET backend.
You can return whatever format of JSON you desire server side as long as you map it correctly in the AutoComplete anonymous callback. The label,value name value pair is good enough for most requirements but do as you will server side with your JSON just map it correctly in the AutoComplete success callback.