您好,我正在尝试使用tribute.js加载数据。但是,我遇到了一个错误Unable to append to values, as it is a function
。目前,我的配置是:
$(document).ready(function () {
var tribute = new Tribute({
values: function (search, cb) {
getUsernames(search, users => cb(users))
},
menuItemTemplate: function (item) {
return '<img src="'+item.original.img.url + '">' + item.original.name + " " + item.original.last;
},
selectTemplate: function (item) {
return '@' + item.original.username;
},
});
tribute.attach(document.getElementById("id_content"));
function getUsernames(search, cb){
var xhr = new XMLHttpRequest();
xhr.open('GET', '/get/user/mentions?q='+search, true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onload = function () {
var data = JSON.parse(xhr.responseText);
cb(data);
if (xhr.readyState == 4 && xhr.status == "200") {
$.each(data, function(i, item) {
tribute.append(0,[
{
name: item.first_name,
last: item.last_name,
username:item.username,
img: item.image
},
]);
});
} else if (xhr.status === 403) {
cb([]);
}
}
xhr.send(null);
}
})
是each
循环的原因,我想知道如何正确地从Ajax加载数据?