好的,我在这里结束了。我查看并复制了我能找到的每个例子,但没有任何作用。这是我的设置
控制器操作:
public async Task<IHttpActionResult> GetRecipeOData(ODataQueryOptions<Recipe> queryOptions)
{
// validate the query.
try
{
queryOptions.Validate(_validationSettings);
}
catch (ODataException ex)
{
return BadRequest(ex.Message);
}
RecipeContext rc = new RecipeContext();
return Ok<IQueryable<Recipe>>(rc.Recipes.AsQueryable());
//return StatusCode(HttpStatusCode.NotImplemented);
}
Kendo Grid:
var initRecipesGrid = function () {
$("#recipesGrid").kendoGrid({
dataSource: {
type: "odata",
transport: {
read: "odata/RecipeOData",
}
},
height: 550,
groupable: true,
sortable: true,
pageable: {
refresh: true,
pageSizes: true,
buttonCount: 5
},
columns: [{
field: "Label",
title: "Recipe Name"
},{
field: "OriginalGravity",
title: "O.G."
},{
field: "FinalGravity",
title: "F.G."
}]
});
}
以下要求:
GET http://localhost:25189/odata/RecipeOData?$callback=jQuery21109926259643398225_1405994583958&%24inlinecount=allpages&%24format=json
返回以下响应:
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
DataServiceVersion: 3.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?Qzpcd29ya3NwYWNlXEJlZXJSZWNpcGVcQmVlclJlY2lwZVdlYlxvZGF0YVxSZWNpcGVPRGF0YQ==?=
X-Powered-By: ASP.NET
Date: Tue, 22 Jul 2014 02:03:22 GMT
Content-Length: 836
{
"odata.metadata":"http://localhost:25189/odata/$metadata#RecipeOData","value":[
{
"Label":"NewCrazy Icelandic Porter","SubmittedById":"b31452d1-d8d7-480c-aa9c-4e298da0054e","StyleId":6,"Notes":"Love this beer","OriginalGravity":1.057,"FinalGravity":1.012,"YeastId":635,"SubmittedOn":"2014-07-17T22:05:59.967"
},{
"Label":"NewSome Crazy Amber Ale","SubmittedById":"b5166f3a-b278-4f2c-8ec5-12376de48405","StyleId":4,"Notes":"Great taste, less filling","OriginalGravity":1.057,"FinalGravity":1.012,"YeastId":1027,"SubmittedOn":"2014-07-17T22:10:17.667"
},{
"Label":"NewCrazy Icelandic Porter","SubmittedById":"30f22f2f-6f28-4a16-8dc8-f3f837887e88","StyleId":9,"Notes":"Love this beer","OriginalGravity":1.057,"FinalGravity":1.012,"YeastId":763,"SubmittedOn":"2014-07-17T22:10:29.353"
}
]
}
我在dataSource中添加了一个“错误”函数,我记录了错误。我得到以下内容:
object {xhr: Object, status: "parsererror", errorThrown: SyntaxError, sender: ct.extend.init, _defaultPrevented: false…}
_defaultPrevented: false
errorThrown: SyntaxError
message: "Unexpected token :"
stack: "SyntaxError: Unexpected token :↵ at eval (native)↵ at Function.jQuery.extend.globalEval (http://localhost:25189/Scripts/jquery-2.1.1.js:330:5)↵ at jQuery.ajaxSetup.converters.text script (http://localhost:25189/Scripts/jquery-2.1.1.js:8654:11)↵ at ajaxConvert (http://localhost:25189/Scripts/jquery-2.1.1.js:7786:19)↵ at done (http://localhost:25189/Scripts/jquery-2.1.1.js:8201:15)↵ at XMLHttpRequest.<anonymous> (http://localhost:25189/Scripts/jquery-2.1.1.js:8598:9)"
get stack: function () { [native code] }
set stack: function () { [native code] }
__proto__: Error
isDefaultPrevented: function (){return this._defaultPrevented===!0}
preventDefault: function (){this._defaultPrevented=!0}
sender: ct.extend.init
status: "parsererror"
xhr: Object
__proto__: Object
答案 0 :(得分:0)
我的DataSource配置嵌套错误。我需要它看起来像这样:
transport: {
read: {
url:"odata/RecipeOData",
dataType: "json"
}