为什么我对Codeigniter 4.21的 AJAX 请求响应OK,但不会绑定到select2

utugiqy6  于 2023-01-06  发布在  其他
关注(0)|答案(1)|浏览(139)
    • 我正在尝试实现AJAX请求到Codeigniter 4.21路由并将其绑定到select2,结果数据正常,但不会绑定到select2。有任何解决方案吗?**
    • 主计长**
public function getSpesialis(){

  
        $response = array();
  
        
        $userlist = $this->spesialisModel->findAll();
        
  
        $data = array();

        foreach($userlist as $user){
           $data[] = array(
              "id" => $user['id'],
              "spesialis" => $user['spesialis'],
           );
        }
  
        $response['data'] = $data;
  
        return $this->response->setJSON($response);
  
     }
    • J查询**
<script>
    $('#spesialis').select2({
        placeholder: "Pilih Spesialisasi...",
        ajax: {
            url: '/getspesialis',
            type: 'GET',
            dataType: 'JSON',
            delay: 250,
            processResults: function(data) {
                return {
                    results: data
                    
                };
            }

        },

    });
pprl5pva

pprl5pva1#

就用这个...谢谢大家

$('#spesialis').select2({
    placeholder: "Pilih Spesialisasi...",
    ajax: {
        url: '/getspesialis',
        type: 'GET',
        dataType: 'JSON',
        delay: 250,
        processResults: function({
            data
        }) {
            return {
                results: $.map(data, function(item) {
                    return {
                        id: item.id,
                        text: item.spesialis
                    }
                })
            }
        }

    },

});

相关问题