我正在关注 Using Kendo UI with MVC4 WebAPI OData and EF文章。安装 KendoUI 并确保设置了所有引用后,我输入了三个字符,并收到以下错误:
Uncaught TypeError: Object # has no method 'slice'
问题的根源
通过更新保存阅读:通过调试我发现问题是 JS 期望解析一个数组,它在数据中不可用 - 在根。在数据层次结构中,它是一个级别。
原始问题
我清理了 kendo.web.min.js,错误发生在第 3498 行附近:
success: function (n) {
var i = this,
r = i.options;
return i.trigger(wt, {
response: n,
type: "read"
}), n = i.reader.parse(n), i._handleCustomErrors(n) ? (i._dequeueRequest(), t) : (i._pristine = et(n) ? e.extend(!0, {}, n) : n.slice ? n.slice(0) : n, i._total = i.reader.total(n), i._aggregate && r.serverAggregates && (i._aggregateResult = i.reader.aggregates(n)), n = i._readData(n), i._pristineData = n.slice(0), i._data = i._observe(n), i._addRange(i._data), i._process(i._data), i._dequeueRequest(), t)
Kendo UI 小部件和 css 加载得很好:
<link href="~/Content/kendo/kendo.common.min.css" rel="stylesheet" />
<link href="~/Content/kendo/kendo.default.min.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.9.1.min.js"></script>
<script src="~/Scripts/kendo/kendo.web.min.js"></script>
<script src="~/Scripts/kendo/kendo.aspnetmvc.min.js"></script>
<script src="~/Scripts/appScripts.js"></script>
而且我在使用 Razor MVC 帮助器/扩展时看到了同样的错误:
@(Html.Kendo().AutoComplete()
.Name("userAutoComplete") // specifies the "id" attribute of the widget
.DataTextField("USERNAME")
.DataSource(source =>
{
source.Read(read =>
{
read.Url("/api/user");
})
.ServerFiltering(true); // if true, the DataSource will not filter the data on the client
}
)
)
并直接通过 JS:
/// <reference path="kendo/kendo.aspnetmvc.min.js" />
/// <reference path="kendo/kendo.core.min.js" />
/// <reference path="kendo/kendo.autocomplete.min.js" />
/// <reference path="kendo/kendo.web.min.js" />
$(document).ready(function () {
// load up KendoUI
// gets data from /api/user
var dataSource = new kendo.data.DataSource({
transport: {
read: {
url: "/api/user"
}
}
});
$("#userSearch").kendoAutoComplete({
dataSource: dataSource,
dataTextField: "USERNAME",
minLength: 3
});
$("#userSearch").on('input', function () {
console.log($("#userSearch").val());
});
}); // $(document).ready()
我确定这是我可能遗漏的简单内容。我已经尝试过使用网络和所有 js 文件。
如有任何帮助,我们将不胜感激。
-- 更新--
该内容中唯一缺少的真实 html 是 <input id="userAutoComplete" />
我基于从 http://api.geonames.org 获取 JSON 数据的 Kendo UI 示例之一,创建了一个全新的解决方案和一个非常简单的 View 。 , 并得到同样的错误。
我认为使用最新的 JS 库(//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js
可能会导致问题,所以我尝试了 1.7 库。同样的问题:
@using Kendo.Mvc.UI
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link rel="stylesheet" href="@Url.Content("~/Content/kendo.common.min.css")">
<link rel="stylesheet" href="@Url.Content("~/Content/kendo.default.min.css")">
<link rel="stylesheet" href="@Url.Content("~/Content/kendo.dataviz.min.css")">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script src="@Url.Content("~/Scripts/kendo.web.min.js")"></script>
<script src="@Url.Content("~/Scripts/kendo.aspnetmvc.min.js")"></script>
<script src="@Url.Content("~/Scripts/kendo.dataviz.min.js")"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#autoComplete").kendoAutoComplete({
minLength: 6,
dataTextField: "title",
filter: "contains",
dataSource: new kendo.data.DataSource({
transport: {
read: {
url: "http://api.geonames.org/wikipediaSearchJSON",
data: {
q: function () {
return $("#autoComplete").data("kendoAutoComplete").value();
},
maxRows: 10,
username: "demo"
}
}
},
schema: {
data: "geonames"
}
}),
change: function () {
this.dataSource.read();
}
})
});
</script>
</head>
<body>
<div>
<input id="autoComplete"/>
</div>
</body>
</html>
-- 更新--
使用上面的代码,我返回并再次尝试 - 它工作正常。多次尝试后,我遇到了同样的问题。这是由于有效的 JSON 数据更改为以下内容:
{"status":{"message":"the daily limit of 30000 credits for demo has been exceeded. Please use an application specific account. Do not use the demo account for your application.","value":18}}
...这让我查看来 self 的 API 的数据格式(在 Fiddler 中查看:
代替:
JSON ---{...数据...
是
JSON
---$id=1
---$values
------{}
---------$id=2
---------CREATEDATETIME...
---------EMAIL=email@email.com
---------GROUPS
------------$id=...
------------$id=...
---------USERNAME=someusername
------{}
---------$id=4
.
.
.
所以错误是由于数组无法在预期的位置访问引起的 - 而不是根,它是一层深。
如何将数据绑定(bind)到一级深度而不是 JSON 对象的根?
谢谢。
最佳答案
我在用作自动完成功能的 ComboBox 上遇到了同样的错误。在我的 Controller 中,返回语句是
return Json(model.ToDataSourceResult(dataSourceRequest), JsonRequestBehavior.AllowGet)
我改成了
return Json(model, JsonRequestBehavior.AllowGet)
这为我提供了根级别的数组,而不是更深的一个级别。
关于javascript - 使用 Javascript/KendoUI 自动完成呈现数据时出错 - 对象 #<Object> 没有方法 'slice' - 如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17836235/