I'm trying to hook up a text field to jQuery UI's autocomplete with a remote data source using a POST query. So far I have this:
$( "#booking_student" ).autocomplete({
source: function( request, respond ) {
$.post( "/path/to/my/api.php", { student: request.term },
function( response ) {
//do something
} );
}
});
Using Firebug I can see that my API is returning the results I'd expect, but the autocomplete dropdown doesn't appear. What do I need to do to plug my results into the autocomplete dropdown? Do I need to populate a variable in the //do something section with the JSON results or?
You need to supply the results to the respond
callback that the widget is giving you:
$( "#booking_student" ).autocomplete({
source: function( request, respond ) {
$.post( "/path/to/my/api.php", { student: request.term },
function( response ) {
respond(response);
});
}
});
This of course assumes that your data is an array with items containing either a label
property, a value
property, or both. This is outlined in the documentation for the source
option.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With