我正在尝试在我的 Laravel 项目上使用 ajax 进行 select2,并且这种情况正在发生:
这是我的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();
我试图找到其他有此问题的人,但没有找到
从配置对象中删除 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,
}
});