Select2 TypeError:data.results未定义

 葛妹秀 发布于 2023-02-13 19:32

我试图使用Select2加载远程数据使用ajax/json但我不断收到错误:

TypeError:data.results未定义

我的代码是:

$('#tags').select2({
                ajax: {
                    url: 'http://localhost:8090/getallusers',
                    dataType: 'json',
                    quietMillis: 100,
                    data: function (term) {
                        return {
                            term: term
                        };
                    },
                    results: function (data) {
                        return data;
                        }

                    }

            });

我真的不明白这个问题!

1 个回答
  • Select2需要将结果作为具有id:和text:attributes的对象的集合.

    喜欢:

    [{'id':1,'text':'Demo'},{'id':2,'text':'Demo 2'}]

    尝试重新格式化您的响应,例如:

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

    2023-02-13 19:34 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有