我正在使用最新版本的JayData与RC的Asp。Net WebAPI OData。当我运行JavaScript函数从服务器获取数据时,我可以看到带有JSON数据的请求和响应。然而,.then()函数永远不会触发,所以UI不知道数据从服务器返回。
这是我的JayData调用:(function(root) {
var context = new Default.Container({
name: 'oData',
oDataServiceHost: '../api/transactions'
});
$(function() {
context.TransactionsByMonth.take(5).toArray().then(function(transactions) {
transactions.forEach(function (transaction) {
alert('Test');
});
});
});
}(window));
以下是响应头:
响应标头cache - control no - cache内容长度1811application/json内容类型日期2012年12月21日星期五格林尼治时间08:32:58到期后1编译指示no - cachemicrosoft iis服务器/8.0X-AspNet-Version 4.0.30319X-Powered-By ASP。网X-SourceFiles = ? utf - 8 B ? YzpcdXNlcnNcanVzdGluXGRvY3VtZW50c1x2aXN1YWwgc3R1ZGlvIDIwMTJcUHJvamVjdHNcVHJlYXNTdXJlLldlYlxUcmVhc1N1cmUuV2ViXGFwaVx0cmFuc2FjdGlvbnNcVHJhbnNhY3Rpb25zQnlNb250aA = = ?=请求头接受application/atomsvc+xml;q=0.8, application/json;odata=verbose;q=0.5, /;q=0.1接受编码gzip, deflate接收语言en - us, en; q = 0.5连接维生饼干glimpseState =零;glimpseLatestVersion = 0.87;glimpseOptions =零;glimpseClientName =零主机localhost: 42039MaxDataServiceVersion 2.0推荐人http://{localhost}/仪表板/测试用户代理Mozilla/5.0 (Windows NT 6.2;WOW64;rv:17.0) Gecko/20100101 Firefox/17.0
下面是响应:
[
{
"TransactionByMonthId": 1,
"BeginningBalanceAmount": 5970.0,
"FloatAmount": 8027.0,
"InflowsAmount": 8607.0,
"OutflowsAmount": -2057.0,
"EndingBalanceAmount": 33345.0,
"MonthOfYear": 12,
"MonthOfYearName": "MonthOfYearName6136f9ba-95c3-4977-ab29-5ec2e2968c5f",
"QuarterOfYear": 8,
"YearNumber": 2012
},
{
"TransactionByMonthId": 10,
"BeginningBalanceAmount": 9201.0,
"FloatAmount": 4723.0,
"InflowsAmount": 4933.0,
"OutflowsAmount": -2808.0,
"EndingBalanceAmount": 9858.0,
"MonthOfYear": 11,
"MonthOfYearName": "MonthOfYearName7c407e0d-d730-43c3-aff6-d3e56313f992",
"QuarterOfYear": 17,
"YearNumber": 2012
},
{
"TransactionByMonthId": 19,
"BeginningBalanceAmount": 3151.0,
"FloatAmount": 19.0,
"InflowsAmount": 9340.0,
"OutflowsAmount": -6118.0,
"EndingBalanceAmount": 19958.0,
"MonthOfYear": 10,
"MonthOfYearName": "MonthOfYearName504ded96-a4e3-4492-ae22-98da7c9c2ba1",
"QuarterOfYear": 26,
"YearNumber": 2012
},
{
"TransactionByMonthId": 28,
"BeginningBalanceAmount": 3826.0,
"FloatAmount": 7120.0,
"InflowsAmount": 3341.0,
"OutflowsAmount": -9360.0,
"EndingBalanceAmount": 33617.0,
"MonthOfYear": 9,
"MonthOfYearName": "MonthOfYearName5fa907a3-b6a3-4a42-a53b-2229da6e1dc6",
"QuarterOfYear": 35,
"YearNumber": 2012
},
{
"TransactionByMonthId": 37,
"BeginningBalanceAmount": 6832.0,
"FloatAmount": 4412.0,
"InflowsAmount": 2462.0,
"OutflowsAmount": -3726.0,
"EndingBalanceAmount": 40857.0,
"MonthOfYear": 8,
"MonthOfYearName": "MonthOfYearNameb3f6a899-6201-418a-b020-31babb42123b",
"QuarterOfYear": 44,
"YearNumber": 2012
}
]
有两件事需要检查:
jquery或q required
如果你想使用承诺接口(然后/fail),那么你应该包括jquery或q.js作为承诺实现(JayData没有自己的承诺实现)。如果你使用q.js,你还必须包括jaydatammodules/qdeferred.js作为承诺适配器(这你不必与jquery做)。
给定的响应是WebAPI而不是OData
您复制的响应负载不是OData,它必须来自一个简单的WebAPI响应。因此,您需要来自JayData的WebAPI提供程序,而不是这种格式的OData提供程序。如果您正在使用ASP。. NET WebAPI OData包,那么有些东西没有正确地连接进来:这个包将WebAPI响应有效载荷格式更改为该OData的格式。
也请捕获并记录失败分支。那就是()fail(函数(原因){console.log(原因);})另外,如果有任何异常发生,你可以用chrome浏览器捕获,所以也请尝试。