选择 2 个“无法加载 ajax 结果”

Select 2 "ajax results could not be loaded"

本文关键字:无法加载 ajax 结果 加载 选择      更新时间:2023-09-26

抱歉,找不到解决方法。

每当我尝试进行一些搜索时,select2 都会显示"无法加载结果"。

我认为我的 ajax 设置是错误的

.html:

<select class="js-data-example-ajax form-control" multiple="multiple"></select>

脚本:

$(".js-data-example-ajax").select2({
    ajax: {
        url: '@Url.Action("LoadCity", "Addresses")',
        dataType: 'jsonp',
        delay: 250,
        data: function(params) {
            return {
                q: params.term, // search term
                page: params.page
            };
        },
        processResults: function(data) {
            return {
                results: data
            };
        },
        cache: true
    },
    minimumInputLength: 1,
});

屏幕


添加 08.07.2016

一些更改 Ajax 设置:

dataType: 'jsonp'

dataType: 'json'

并添加

type: 'GET',

现在没有消息"无法加载结果",也没有结果

您上一条评论的基础。流程结果应返回具有结果键的对象。

所以它应该是:

return {
   results: [{id: 1, text: 'Test'}]
}

>我最近在使用 4.0.5 版时遇到了完全相同的问题

这是从版本 4.0.6 开始解决的组件中的已知错误

来自Github官方仓库:

修复 AJAX 数据源错误 #4356

更新我的本地版本的 select2 组件解决了这个问题。

我有这个工作选择2,我昨天已经实现了这个,它可能会对你有所帮助。

        select2_common('.accounts','Account & Description');
        function select2_common(selector,placeholder)
        {
            $(selector).select2({
                minimumInputLength:2,
                placeholder:placeholder,
                ajax: {
                    url: "your_url_here",
                    dataType: "json",
                    delay: 200,
                    data: function (params) {
                        return {
                        q: params.term, // search term
                        page: params.page
                    };
                },
                processResults: function (data) {
                    // console.log(data);
                    return {
                        results: $.map(data, function(obj) {
                            if(obj.id!=0){
                            // console.log(obj);
                            return { id: obj.id, text: obj.name };
                        }
                            else
                                {return {id: obj.id, text: obj.name}}
                        })
                    };
                },
                cache: true
            },
            debug:false
        });
    }

//And your result should be in this form, from your method....
//I am using laravel php framework.

public function getAccountDetail(Request $request)
{
    $q = $request->input('q');
    $result=[];
    if (isset($q) && $q != '') {
            /*----------  Search by account code or title  ----------*/
        $data = DB::table('acc_accounts')->select('acc_id','acc_code','acc_title')
        ->where('acc_code', 'like', '%' . $q . '%')
        ->orWhere('acc_title', 'like', '%' . $q . '%')
        ->limit(10)->orderBy('acc_code')->get();
          foreach ($data as $key => $value) {
            $new1 = array('id' => $value->acc_id, 'name' => $value->acc_code.' ('.$value->acc_title.')' );
            array_push($result,$new1);
          }
        print(json_encode($result));
    }
}