【问题标题】:Resolving Error in resource configuration解决资源配置中的错误
【发布时间】:2014-05-27 16:12:07
【问题描述】:

我一直在通过以下教程来了解 AngularJS: http://jphoward.wordpress.com/2013/01/05/end-to-end-web-app-in-under-an-hourpart-3/

改编本教程后,我收到以下错误,正在努力寻找解决方案。我的理解是它与工厂有关,最初我认为这可能是一个格式错误的 API 请求,但我似乎在浏览器中得到了正确的结果。

错误:[$resource:badcfg] 资源配置错误。预期响应包含一个对象但得到一个数组

app.js 代码为:

//Main App
var app = angular.module("FixturesApp", ['ngRoute', 'ngResource']).
config(function ($routeProvider) {

    //Routing
    $routeProvider
        .when('/', { templateUrl: 'list.html', controller: ListCtrl })
        .otherwise({ redirectTo: '/' });
});


//Factory
app.factory('Fixture', function ($resource) {
return $resource('/api/fixtures/:id', { id: '@id'}, { update: { method: 'PUT' } });
});

//Directive
app.directive('sorted', [
function() {
    return {
        scope: true,
        restrict: 'A',
        transclude: true,
        template: '<a class="btn btn-link" ng-click="do_sort()" ng-transclude></a>' +
            '<span ng-show="do_show(true)">' +
            '<i class="glyphicon glyphicon-arrow-down"></i>' +
            '</span>' +
            '<span ng-show="do_show(false)">' +
            '<i class="glyphicon glyphicon-arrow-up"></i>' +
            '</span> ',
        controller: function($scope, $element, $attrs) {
            $scope.sort_by = $attrs.sorted;

            $scope.do_sort = function() {
                $scope.sort($scope.sort_by);
            };

            $scope.do_show = function(is_desc) {
                return (is_desc != $scope.is_desc && $scope.sort_order == $scope.sort_by)
            }
        }
    };
}
]);

//Controller
var ListCtrl = function ($scope, $location, Fixture) {
$scope.sort_order = "Id";
$scope.is_desc = false;

$scope.sortString = function (is_desc) {
    return is_desc ? "-" : "";
}

$scope.sort = function (col) {
    if ($scope.sort_order === col) {
        $scope.is_desc = !$scope.is_desc;
    } else {
        $scope.is_desc = false;
        $scope.sort_order = col;
    }
    $scope.search($scope.is_desc);
}

$scope.search = function (is_desc) {
    var fixtureList = Fixture.get({ order: $scope.sortString(is_desc) + $scope.sort_order }, function() {
        $scope.items = fixtureList.results;
    });
}

$scope.search(false);

}

【问题讨论】:

  • 您尝试连接的 API 是什么?它返回什么?你能给出 /api/fixtures/:id 的完整路径吗?
  • 完整的 URL 是localhost:1062/api/fixtures。我还没有发布它,我正在使用 MVC 的 Web API。

标签: angularjs


【解决方案1】:

您的 API 返回的字符串似乎不是有效的 JSON(JavaScript OBJECT 表示法)。 JSON 由键:值对组成。例如:

{"myArray" : 
    [
    {"name" : "John"},
    {"name" : "Sarah"}
    ]
}

您不能直接使用数组 => 这是无效的 JSON:

[
    {"name" : "John"},
    {"name" : "Sarah"}
]

简而言之,您的字符串至少应该以{ 开头,而不是[

有关 JSON 格式的完整说明,请参阅 http://json.org/

【讨论】:

  • 感谢 CCH,这是我浏览到 /api/fixtures 时的输出示例:[{"Id":1,"FixtureLeague":"League A","FixtureSeason":"2014 ","FixtureDate":"2014-05-05T00:00:00","Fixture":"Pyecombe v Haywards Heath","FixtureResult":null},{"Id":2,"FixtureLeague":"联赛 A ","FixtureSeason":"2014","FixtureDate":"2014-07-05T00:00:00","Fixture":"Worthing v Dale Hill","FixtureResult":null}]
  • 我现在看到了问题,当返回单个记录时 (localhost:1062/api/fixtures/1) 它错过了方括号 []
猜你喜欢
  • 2014-01-01
  • 2018-03-24
  • 2018-04-05
  • 1970-01-01
  • 2020-01-09
  • 1970-01-01
  • 2020-10-22
  • 2020-05-15
  • 1970-01-01
相关资源
最近更新 更多