尝试使用角资源从API中提取项目列表。如果我从浏览器点击并使用swagger,我会得到一个json项目列表。
但无法通过角度上的错误输出页面上的结果列表。
.factory('ProjectsDS', ['$resource','$timeout',
function($resource, $timeout){console.log(0);
var proj = $resource('/projects');
$timeout(2000);
console.log( 0 + ' ' + proj.query().length);
return $resource('/projects')}])浏览器控制台中的错误:
"Error: [$resource:badcfg] http://errors.angularjs.org/1.5.5/$resource/badcfg?p0=Error%20in%20resource%20configuration%20for%20action%20%60%7B0%7D%60.%20Expected%20response%20to%20contain%20an%20%7B1%7D%20but%20got%20an%20%7B2%7D%20(Request%3A%20%7B3%7D%20%7B4%7D)&p1=query&p2=array&p3=object&p4=GET&p5=%2Fprojects
O/<@http://localhost:8000/js/angular.min.js:6:412
resourceFactory/</Resource[name]/promise<@http://localhost:8000/js/angular-resource.js:682:25
e/<@http://localhost:8000/js/angular.min.js:130:226
tf/this.$get</n.prototype.$eval@http://localhost:8000/js/angular.min.js:144:463
tf/this.$get</n.prototype.$digest@http://localhost:8000/js/angular.min.js:142:39
tf/this.$get</n.prototype.$apply@http://localhost:8000/js/angular.min.js:145:247
l@http://localhost:8000/js/angular.min.js:97:53
H@http://localhost:8000/js/angular.min.js:101:190
dg/</u.onload@http://localhost:8000/js/angular.min.js:102:229
"我认为,使用超时来处理初始的空返回对象。长度输出为0。
API如果浏览到:
{"_links":{"self":{"href":"http:\/\/localhost:8000\/projects?page=1"},"first":{"href":"http:\/\/localhost:8000\/projects"},"last":{"href":"http:\/\/localhost:8000\/projects?page=1"}},"_embedded":{"projects":[{"id":"1","message":null,"timestamp":null,"user":null,"name":"First 1","site":"https:\/\/www.nathanhaley.com","description":"This is one description.","_links":{"self":{"href":"http:\/\/localhost:8000\/projects\/1"}}},{"id":"2","message":null,"timestamp":null,"user":null,"name":"First 1","site":"https:\/\/www.nathanhaley.com","description":"This is one description.","_links":{"self":{"href":"http:\/\/localhost:8000\/projects\/2"}}},{"id":"3","message":null,"timestamp":null,"user":null,"name":"First 2","site":"https:\/\/www.nathanhaley.com","description":"This is one description.","_links":{"self":{"href":"http:\/\/localhost:8000\/projects\/3"}}},{"id":"4","message":null,"timestamp":null,"user":null,"name":"First 12","site":"https:\/\/www.nathanhaley.com","description":"This is one description.","_links":{"self":{"href":"http:\/\/localhost:8000\/projects\/4"}}}]},"page_count":1,"page_size":25,"total_items":4,"page":1}谢谢你的帮助。
发布于 2016-05-16 16:56:23
根据记录的错误:
当$resource服务期望将响应反序列化为数组,但接收对象时,则会发生此错误,反之亦然。
在这一行上:console.log( 0 + ' ' + proj.query().length);尝试使用.get()而不是.query()
https://stackoverflow.com/questions/37258927
复制相似问题