I am making an Ajax request to a music search API, using the jQuery autocomplete feature. The call works great, but I believe I have to do some Ajax adjustments/enhancements. The autocomplete feature seems to work great every time the space bar is clicked, which is quite odd?
If I type in the letters "lose", I get the full song list output to my console, but the dropdown suggestion box does not show, unless I hit the space bar, or if I hit the down arrow on my keyboard?
function songSelection() {
$(".song-input").each(function () {
let input = $(this);
if (input.next() == 'SongSelector') {
let timer;
input.on('keydown', function () {
timer = setTimeout(doneTyping, 300);
});
function doneTyping() {
let songSearch = input.val()
$.ajax({
url: `https://deezerdevs-deezer.p.rapidapi.com/searchq=${songSearch}`,
method: "GET",
headers: {
"x-rapidapi-host": "deezerdevs-deezer.p.rapidapi.com",
"x-rapidapi-key": "APIkeyhere"
},
success: function (data) {
let songList = []
for (let item in data.data) {
songList.push(data.data[item].song)
}
$('.song-input').autocomplete({
source: songList,
})
},
})
}
}
});
}
Am I missing something in the Ajax request? Just seems like pretty odd functionality to me that the dropdown autocomplete suggestions seem to really only want to work when the spacebar or down arrow key is hit after typing?