laravel Select2 类型错误:b 未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39952164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:35:12  来源:igfitidea点击:

Select2 TypeError: b is undefined

javascriptphpjquerylaravelselect2

提问by Aamir

enter image description hereenter image description hereI'm using select2 to show ajax results in a dropdown but when I append data to select2 its showing error

在此处输入图片说明在此处输入图片说明我正在使用 select2 在下拉列表中显示 ajax 结果,但是当我将数据附加到 select2 时,它的显示错误

TypeError: b is undefined

JS Code

JS代码

        var baseurl = $("#baseurl").val();
        $(".myselect").select2({
            placeholder: "Select a inspector",
            allowClear: true,
            ajax: {
                url: baseurl + '/admin/getdata',
                dataType: 'json',
                type: "GET",
                quietMillis: 50,
                data: function (term) {
                    return {
                        term: term.term
                    };
                },
                results: function (data) {
                    var myResults = [];
                    $.each(data, function (index, item) {
                        myResults.push({
                            'id': item.id,
                            'text': item.firstname
                        });
                    });
                    return {
                        results: myResults
                    };
                }
            }
        });

term.term contains the value of input text in dropdown search box.

term.term 包含下拉搜索框中输入文本的值。

HTML

HTML

  <select class="myselect" style="width: 50% !important">
        <option></option>
        <option value="AL">Alabama</option>
        <option value="WY">Wyoming</option>
        <option value="KY">Kentucky</option>
  </select>

JSON RESPONSE

JSON 响应

[{"id":9858,"firstname":"Testing3","status":2,"state":"VA","phone":""},{"id":9857,"firstname":"Testing2","status":2,"state":"VA","phone":""},{"id":9856,"firstname":" david polosky ","status":3,"state":"FL","phone":"(000)000-4141"}]

SELECT2 CDN LINKS

选择 2 个 CDN 链接

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>

PHP SERVERSIDE CODE (LARAVEL)

PHP 服务器端代码 (LARAVEL)

 $searchtext = $request->get('term');
 $data = Inspector::latest('id')
                ->select('id', 'firstname', 'status', 'state', 'phone')
                ->where('firstname', 'LIKE', '%' . $searchtext . '%')
                ->get()->toArray();
 echo json_encode($data);

Any help is appreciated.

任何帮助表示赞赏。

回答by R.K.Saini

In your ajax configuration you use results you should use processResults

在你的 ajax 配置中你使用 results 你应该使用 processResults

Try this

尝试这个

var baseurl = $("#baseurl").val();
    $(".myselect").select2({
        placeholder: "Select a inspector",
        allowClear: true,
        ajax: {
            url: baseurl + '/admin/getdata',
            dataType: 'json',
            type: "GET",
            quietMillis: 50,
            data: function (term) {
                return {
                    term: term.term
                };
            },
            processResults: function (data) {
                var myResults = [];
                $.each(data, function (index, item) {
                    myResults.push({
                        'id': item.id,
                        'text': item.firstname
                    });
                });
                return {
                    results: myResults
                };
            }
        }
    });

回答by Sreekanth

Aamir@, sometimes errors are misleading. If you see from your code, it doesn't have a reference to b at all. I believe that must be an error that is being thrown from a method outside of your code, due to an error in your code.

Aamir@,有时错误具有误导性。如果您从代码中看到,它根本没有对 b 的引用。我相信这一定是由于代码中的错误而从代码之外的方法抛出的错误。

You might have to set a break point by clicking on the line number on browser console and then check the call stack to find the error in the code.

您可能必须通过单击浏览器控制台上的行号来设置断点,然后检查调用堆栈以查找代码中的错误。