André Luiz
André Luiz

Reputation: 7302

What is the right JSON format for Select2 remote

I'm a bit lost with the JSON format I must return for Select2 so it works.

My returned JSON captured with fiddler:

Printscreen of my fiddler

And my Select2 setup (#FuncionarioID is a select input):

$('#FuncionarioID').select2({
ajax: {
    dataType: "json",
    url: "Ajax.ashx?Action=FuncionarioSelect2",
    results: function (data) {
        return { results: data };
    },
    cache:false
}

});

I've been reading questions about the same subject here in Stackoverflow, I tried the answers and none of them worked for me. Would you have any tip about solving this problem? I've already tried to return objects like this {"id":"1060","text":"teste 1"} and it didn't work too.

Thanks in advance for any help.

Upvotes: 1

Views: 223

Answers (1)

Guilherme Câmara
Guilherme Câmara

Reputation: 36

Try updating your function like this:

     $('#FuncionarioID').select2({
        ajax: {
            dataType: "json",
            url: "Ajax.ashx?Action=FuncionarioSelect2",
            cache: false,
            processResults: function (data, page) {
                return {
                    results: data
                };
            }
        }
    });

Upvotes: 1

Related Questions