admin管理员组文章数量:1328953
I am getting following error in angular $resource: error description
Error: error:badcfg
Response does not match configured parameter:
Error in resource configuration for action `array`. Expected response to contain an object but got an {2}
I initialized the ng app as follows:
var appRoot = angular.module('smapp', ['ngRoute', 'ui.bootstrap', 'ngResource']);
The service:
appRoot.factory('ProgramsResource', function ($resource) {
return $resource('Home/Program', {}, { Program: { method: 'get', isArray: false } })
});
In my controller:
appRoot.controller('ProgramCtrl', function ($scope, ProgramsResource) {
$scope.searchPrograms = function () {
$scope.Programs = ProgramsResource.query(
{
TotalItems: $scope.TotalItems,
ItemsPerPage: $scope.ItemsPerPage,
PageNo: $scope.CurrentPage
});
};
$scope.TotalItems = 175;
$scope.ItemsPerPage = 20;
$scope.CurrentPage = 1;
$scope.searchPrograms();
});
Json I am sending from the server in respons:
{"TotalItems":175,"ItemsPerPage":20,"PageNo":5,"List":[{"Code":"MATH2014","Name":"Name1","Tags":"Tag1,Tag2"},{"Code":"MATH2015","Name":"Name2","Tags":"Tag1,Tag2"}]}
The angular $response throws error for above json
But if I do not send "List" array within json and send simple json as follows, everything works fine then:
[{"TotalItems":0,"ItemsPerPage":0,"PageNo":0},{"TotalItems":0,"ItemsPerPage":0,"PageNo":0}}]
I am new to angular and don't know what exactly I am doing wrong.
I am getting following error in angular $resource: error description
Error: error:badcfg
Response does not match configured parameter:
Error in resource configuration for action `array`. Expected response to contain an object but got an {2}
I initialized the ng app as follows:
var appRoot = angular.module('smapp', ['ngRoute', 'ui.bootstrap', 'ngResource']);
The service:
appRoot.factory('ProgramsResource', function ($resource) {
return $resource('Home/Program', {}, { Program: { method: 'get', isArray: false } })
});
In my controller:
appRoot.controller('ProgramCtrl', function ($scope, ProgramsResource) {
$scope.searchPrograms = function () {
$scope.Programs = ProgramsResource.query(
{
TotalItems: $scope.TotalItems,
ItemsPerPage: $scope.ItemsPerPage,
PageNo: $scope.CurrentPage
});
};
$scope.TotalItems = 175;
$scope.ItemsPerPage = 20;
$scope.CurrentPage = 1;
$scope.searchPrograms();
});
Json I am sending from the server in respons:
{"TotalItems":175,"ItemsPerPage":20,"PageNo":5,"List":[{"Code":"MATH2014","Name":"Name1","Tags":"Tag1,Tag2"},{"Code":"MATH2015","Name":"Name2","Tags":"Tag1,Tag2"}]}
The angular $response throws error for above json
But if I do not send "List" array within json and send simple json as follows, everything works fine then:
[{"TotalItems":0,"ItemsPerPage":0,"PageNo":0},{"TotalItems":0,"ItemsPerPage":0,"PageNo":0}}]
I am new to angular and don't know what exactly I am doing wrong.
Share Improve this question asked Sep 29, 2014 at 5:21 Saurabh PalatkarSaurabh Palatkar 3,38412 gold badges52 silver badges112 bronze badges1 Answer
Reset to default 8Instead of doing
$scope.Programs = ProgramsResource.query(
Use
$scope.Programs = ProgramsResource.get(
query
function expects the response to be an array, where as get
expects a object. Since you are returning object use get
.
The default setting for query function is isArray:true
. This flag helps angular to de-serialize your response into either object or array. See resource documentation.
Also note:
When you change default settings for a query function like the following, you will encounter this error if you do not define isArray
as true
. So always add isArray: true
when you change the default settings for query
:
var res = $resource('/api/userinfoes/:Id', { Id: "@Id" },
{
'query': {
method:'GET',
headers: {
'Authorization': 'Bearer ' + token
},
isArray:true}
});
本文标签: javascriptResponse does not match configured parameterStack Overflow
版权声明:本文标题:javascript - Response does not match configured parameter: - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742223178a2435580.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论