json php 对 select2 的响应未呈现

json response from php to select2 not rendering

我正在尝试使用 select2 从 PHP 后端使用 AJAX 提取数据。我无法像我想的那样弄清楚文档。我想我可能错过了一些理所当然的东西。
我是这样开始的:

HTML

<select id="select_proj" style="width:10em">
  <option value="" selected="selected">Search</option>
</select>

js

$('select').select2();
$("#select_proj").select2({
    ajax : {
        url : '../app/select_prj.php',
        dataType : 'json',
        delay : 250,
        data : function (term, page) {
            return {
                select_proj: term, // search term
                page: 10
            };
        },
        processResults: function (data, page) {
            return {
                results: data.items
            };
        },
        cache: true
    },
    escapeMarkup : function (markup) { return markup; }, // let our custom formatter work
    minimumInputLength : 1,
});

在PHP

json 对象从 PHP

返回
echo json_encode($result_data);

数据看起来像

[{
    "PROJ_ID" : 10039,
    "0" : 10039
},{
    "PROJ_ID" : 10042,
    "0":10042
}] 

但是 select 框中除了消息 "No results found".
之外什么也没有发生 我错过了什么?

您的回复必须如下所示:

[
    { id: 0, text: 'enhancement' },
    { id: 1, text: 'bug' }, 
    { id: 2, text: 'duplicate' },
    { id: 3, text: 'invalid' }, 
    { id: 4, text: 'wontfix' }
]
  • "id" 将是选项值
  • "text" 将是选项标签

并且您必须从 "data.items" 中删除“.items”,因为您的回复中没有密钥 "items"。