my parameter in autocomplete function isn’t recognized in jquery-ui when i do a console.log, i see an autocomplete-url and [Object object] after
that’s my code :
`import algoliasearch from ‘algoliasearch/lite’;
import { autocomplete, getAlgoliaResults } from ‘@algolia/autocomplete-js’;
const searchClient = algoliasearch(
‘latency’,
‘6be0576ff61c053d5f9a3225e2a90f76’
);
$(document).ready(function() {
$("#search").each(function () {
var autocompleteUrl = $(this).attr('autocomplete-url');
console.log('autocomplete vaut ' + autocompleteUrl);
$("#search").autocomplete({hint: false}, {
source: function(query,cb){
$.ajax({
url: autocompleteUrl
}).then(function (data) {
console.log(data);
});
console.log(autocompleteUrl+query);
},
},
)
});
});
`
i try have my response of my request .
4
Answers
the code just display the result in the console, not under the input search bar.It's not query but query.term , we can know it when we dsiplay the object query.
The jQuery UI Autocomplete documentation specifies that the
source
value, when a function, will be called with two arguments:request
andresponse
.request
is an object that contains theterm
, as you have identified.response
is a callback that will be called with the array of values you want to have appear in the dropdown.Assuming our API response is an array of objects, each contains a
nom
property, we will need to map that into an array of strings (or, optionally, an array of objects withlabel
andvalue
properties) and invoke theresponse
callback, passing it the mapped values.my response is brut data , this is the image
so , i resume for the viewers:
-remove the appendTo attribute
And is not necessary to use JSON data .In this case the api return brut data
so , the code is :
$(document).ready(function() {
})