Select2 TypeError:无法使用“in”运算符在ajax请求中搜索未定义的“状态”

问题描述 投票:0回答:1

我正在尝试在我的 Laravel 项目上使用 ajax 进行 select2,并且这种情况正在发生:

error in the console

error line

这是我的js:

import 'select2';

let baseurl = window.location.origin;

$(window).on('load', function () {
    $('#produtos-select2').select2({
        multiple: true,
        ajax: {
            url: baseurl + "produtos/ajax",
            dataType: 'json',
            delay: 250,
            type: 'get',
            data: function (params) {
                return {
                    nome: params.term
                }
            },
            processResults: function (data) {
                console.log(data)
            }, 
            cache: true,
        },
        status: 0
    });
});

路线的动作有这样的返回:

return Produto::select('id', 'nome', 'valor')->get();

我试图找到其他有此问题的人,但没有找到

javascript laravel jquery-select2
1个回答
0
投票

从配置对象中删除 status: 0 行,因为它不是 Select2 的有效选项,并且修改 processResults 函数以正确结构返回结果。 Select2 期望结果位于结果属性内

$('#produtos-select2').select2({
    multiple: true,
    ajax: {
        url: baseurl + "produtos/ajax",
        dataType: 'json',
        delay: 250,
        type: 'get',
        data: function (params) {
            return {
                nome: params.term
            }
        },
        processResults: function (data) {
            return {
                results: data // Make sure the data structure matches what Select2 expects
            };
        },
        cache: true,
    }
});

© www.soinside.com 2019 - 2024. All rights reserved.