Select2 未显示结果,但成功返回 JSON
Select2 not showing results, but successfully returning JSON
我在 MVC4 中使用 Select2 jQuery 插件。我的 JavaScript 文件在我的控制器中的 JSON 中获取数据,但是当我调试并尝试显示结果时出现错误:
Unable to get property 'toUpperCase' of undefined or null reference
我认为这可能与我返回的 JSON 有关,因为它看起来像对象或它们是整数的事实,但我不完全确定。通过 ToString() 将它们转换为字符串也不起作用。
[{"name":1,"label":1,},{"name":2,"label":2},...]
这是我的 JS:
$("#ItemID").select2({
...
ajax: {
url: $("#ItemID").attr("data-getids"),
dataType: "json",
data: function (param) {
return {query: param};},
results: function(data) {
return {results: data};}
}
});
我明白了。我使用的是旧版本的 select2 (3.4.5),我最终在结果函数中映射了 JSON。
results: function (data) {
return {
results: $.map(data, function (item) {
return {text: item.name,
id: item.id};
};
}),
}
我在 MVC4 中使用 Select2 jQuery 插件。我的 JavaScript 文件在我的控制器中的 JSON 中获取数据,但是当我调试并尝试显示结果时出现错误:
Unable to get property 'toUpperCase' of undefined or null reference
我认为这可能与我返回的 JSON 有关,因为它看起来像对象或它们是整数的事实,但我不完全确定。通过 ToString() 将它们转换为字符串也不起作用。 [{"name":1,"label":1,},{"name":2,"label":2},...]
这是我的 JS:
$("#ItemID").select2({
...
ajax: {
url: $("#ItemID").attr("data-getids"),
dataType: "json",
data: function (param) {
return {query: param};},
results: function(data) {
return {results: data};}
}
});
我明白了。我使用的是旧版本的 select2 (3.4.5),我最终在结果函数中映射了 JSON。
results: function (data) {
return {
results: $.map(data, function (item) {
return {text: item.name,
id: item.id};
};
}),
}