0

Hello I am trying to use to load data. However, I face an error, Unable to append to values, as it is a function. Currently my configuration is:

$(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);
    }

})

Which the eachLô đề online loop is causing the issue, I was wondering how I can load the data from Ajax correctly?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policyLô đề online

Browse other questions tagged or ask your own question.