【问题标题】:Select 2 not populating options, but return data is correct format?选择 2 不填充选项,但返回数据格式正确?
【发布时间】:2020-05-25 01:40:40
【问题描述】:

我有这个 jQuery

$('#document').ready(function(){
        $('#page_posts_select').select2({
            ajax: {
                url: "admin/pages/get_posts_for_select",
                type: "GET",
                success: function(data){
                    console.log(data);
                }
            }
        });
    })

而控制台中记录的数据是这样的:

{"results":[{"id":"1","text":"Option1"},{"id":"2","text":"Option2"}]}

我的服务器端函数如下所示:

$results = $this->posts_model->get_all_posts_for_dropdown();
    $select_results = array();
    foreach($results as $result){
        $select_results[] = array('id'=>$result->post_id, 'text'=>$result->title);
    }
    $output = new stdClass();
    $output->results = $select_results;
    echo json_encode($output);

为什么我的选择列表不显示任何选项,它不应该在下拉列表中显示 Option1 和 Option2 吗? ajax 需要一些额外的属性吗?

根据我所做的研究,我只需要正确的返回格式,即具有“结果”属性的对象,该对象包含“id”和“text”字段的数组。

请帮忙,谢谢!

【问题讨论】:

    标签: php json jquery-select2


    【解决方案1】:

    我想我已经解决了这个问题,我没有为 ajax 属性包含 dataType: 'json'

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-15
      • 2018-06-09
      相关资源
      最近更新 更多