响应与配置的参数不匹配: [英] Response does not match configured parameter:

查看:120
本文介绍了响应与配置的参数不匹配:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在角度$ resource中遇到以下错误: 错误描述

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}

我按如下所示初始化了ng应用程序:

I initialized the ng app as follows:

var appRoot = angular.module('smapp', ['ngRoute', 'ui.bootstrap', 'ngResource']);

服务:

appRoot.factory('ProgramsResource', function ($resource) {
    return $resource('Home/Program', {}, { Program: { method: 'get', isArray: false } })
});

在我的控制器中:

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我正在从服务器发送响应:

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"}]}

上面的json的角度$ response引发错误

The angular $response throws error for above json

但是,如果我没有在json中发送列表"数组并按以下方式发送简单的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.

推荐答案

代替做

$scope.Programs = ProgramsResource.query(

使用

$scope.Programs = ProgramsResource.get(

query函数期望响应为数组,而get期望为对象.由于您要返回对象,请使用get.

query function expects the response to be an array, where as get expects a object. Since you are returning object use get.

查询功能的默认设置为isArray:true.此标志有助于angular将您的响应反序列化为对象或数组.参见资源文档.

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.

也请注意: 当您更改如下所示的 query 函数的默认设置时,如果未将isArray定义为true,则会遇到此错误.因此,在更改query的默认设置时,请始终添加isArray: true:

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}
            });

这篇关于响应与配置的参数不匹配:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆