Select2 not getting data via AJAX

为君一笑 提交于 2019-12-02 18:28:01

Try

$('#Organisation').select2({
    ajax: {
        url: 'data.json',
        dataType: 'json',
        quietMillis: 100,
        data: function (term) {
            return {
                term: term
            };
        },
        results: function (data) {
          var results = [];
          $.each(data, function(index, item){
            results.push({
              id: item.ID,
              text: item.label
            });
          });
          return {
              results: results
          };
        }
    }
});

Demo: Plunker

Other than above solution you can do one thing, instead of returning following json

[{"label":"Organisation 1","ID":2},{"label":"Organisation 2","ID":1}]

return this

[{"text":"Organisation 1","id":2},{"text":"Organisation 2","id":1}]

faced the same problem and figured this out after looking at few solutions proposed by other.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!