Select2 TypeError: data.results is undefined
javascript, jquery-select2
Solution
Select2 needs the results as a collection of objects with id: and text: attributes.
Like:
[{ 'id': 1, 'text': 'Demo' }, { 'id': 2, 'text': 'Demo 2'}]
Try, to reformat you response like:
$('#tags').select2({
ajax: {
url: 'http://localhost:8090/getallusers',
dataType: 'json',
quietMillis: 100,
data: function (term) {
return {
term: term
};
},
results: function (data) {
var myResults = [];
$.each(data, function (index, item) {
myResults.push({
'id': item.id,
'text': item.first_name + " " + item.last_name
});
});
return {
results: myResults
};
}
}
});
Problem
Am trying to use Select2 to load remote data using ajax / json but i keep getting an error as: TypeError: data.results is undefined My code is : ``` $('#tags').select2({ ajax: { url: 'http://localhost:8090/getallusers', dataType: 'json', quietMillis: 100, data: function (term) { return { term: term }; }, results: function (data) { return data; } } }); ``` I really Don’t Understand the Problem !