[$resource:badcfg] Error in resource configuration. Expected response to contain an array but got an object?
// Service
angular.module('admin.services',['ngResource'])// GET TASK LIST ACTIVITY.factory('getTaskService',function($resource){return $resource('../rest/api.php?method=getTask&q=*',{'get':{method:'GET'}});})
// Controller
$scope.getTask = getTaskService.query(function(response){ angular.forEach(response,function(item){if(item.numFound >0){for(var i =0; i < item.numFound; i++){ $scope.getTasks[i]= item.docs[i];}}});});
回答1:
First of all you should configure $resource in different manner: without query params in the URL. Default query parameters may be passed as properties of the second parameter in resource(url, paramDefaults, actions). It is also to be mentioned that you configure get method of resource and using query instead.
Service
angular.module('admin.services',['ngResource'])// GET TASK LIST ACTIVITY.factory('getTaskService',function($resource){return $resource('../rest/api.php',{ method:'getTask', q:'*'},// Query parameters{'query':{ method:'GET'}});})
In order to handle arrays with the $resource service, it's suggested that you use the query method. As you can see below, the query method is built to handle arrays.
Make sure you are sending the proper parameters too. This happened to me after switching to UI-Router.
To fix it, I changed $routeParams to use $stateParams in my controller. The main issue was that $stateParams was no longer sending a proper parameter to the resource.