In my laravel application I'm facing a problem with typeahead.js autocomplete when using source with ajax, the suggestion menu does not get displayed, however when the source refers to a div with all my tags (separated by comma) works perfectly, I mean this way it will work:
<div id="available-tag-data" class="tag-data">
adventure, literature, poertry
</div>
and inside JS:
source: substringMatcher(parseTags('available-tag-data'))
and this way it doesn't show the suggestion menu (no console erorrs):
source: function (query, result) {
$.ajax({
url: "/get-tag",
data: 'query=' + query,
dataType: "json",
type: "post",
success: function (data) {
console.log(data)
let myData = []
for (let i = 0; i < data.options.length; i++) {
myData.push(data.options[i].name)
}
console.log(myData)
result($.map(myData, function (item) {
return item
})
)
}
})
}
the json retrieved by "/get-tag"
url:
"{"options":[{"name":"adventure"},{"name":"literature"},]}"
also this does not work:
success: function (data) {
console.log('data', data)
var newData = [];
$.each(data, function () {
newData.push(this.name);
});
result(newData);
return;
}
response:
["adventure","literature"]
Sounds like if content is already preloaded such as in the first example or in an already populated JS array it will work (I already tried to override the array when the new data comes with ajax), but not sure, what am I doing wrong? how the json response should be?? thanks