我知道这个问题已被多次询问过。我有这个错误:
Response for getList SHOULD be an array and not an object or something else
问题是我的回复是一个数组
[{"id":1,"token":"HOME","traductions":[{"id":1,"value":"accueil","langue":{"id":1,"langue":"fran\u00e7ais","locales":"fr_FR"}},{"id":2,"value":"home","langue":{"id":2,"langue":"anglais","locales":"en_EN"}}]},{"id":2,"token":"CONTACT","traductions":[{"id":3,"value":"contact","langue":{"id":1,"langue":"fran\u00e7ais","locales":"fr_FR"}}]}]
这是我的简单控制器
traduction.controller('traductionController', ['$scope', 'Restangular', function($scope, Restangular) {
var containers = Restangular.all('intranetBS/web/app_dev.php/interne/traductions'); // correct route, checked it
containers.getList().then(function(stuff) {
$scope.containers = stuff;
});
}]);
任何解决方案? 非常感谢!!
答案 0 :(得分:2)
.config(function(RestangularProvider){
RestangularProvider.setBaseUrl('/api');
RestangularProvider.addResponseInterceptor(function(data, operation, what, url, response, deferred) {
var extractedData;
if (operation === "getList") {
extractedData = data.traductions;
} else {
extractedData = data;
}
return extractedData;
});
})