I'm using Rails 5 with JQuery UI's autocomplete feature. I'm using AJAX to get the appropriate suggestions for my textbox ...
$(function() {
return $('#s').autocomplete({
source: function (request, response) {
$.get("/people/search.js", {
s: request.term
}, function (responseStr) {
data = eval(responseStr);
// assuming data is a JavaScript array such as
// [{"value": "SpongeyB", "label": "some label" }, {"value": "SpongeyB", "label": "some label" }...]
// and not a string
var jsonData = new Array();
data.forEach(function (item) {
var jsonItem = new Object();
jsonItem.value = item;
jsonItem.label = item;
var myString = JSON.stringify(jsonItem);
jsonData.push( myString );
});
response(jsonData);
});
}
});
});
However, when I type something, waht is appearing under my textbox is not a list of options, but rather the entire jsonData as a string ...
How do I tell the autocomplete function to display each item the JSON list on its own line instead of the entire JSON string?
Edit: In response to the answer given I change my line
jsonData.push( myString );
to
jsonData.push( jsonItem );
but I'm still getting only a single line containing all items appearing under my textbox ...
responseStr
is actually a string and not a js object. I assume you've tried passingdata
intoresponse
unchanged?