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

2024-03-14

对于角度而言,我是一个十足的菜鸟,刚刚完成了代码学校教程,我就遇到了第一个障碍。

我正进入(状态Error: [$resource:badcfg] Error in resource configuration. Expected response to contain an array but got an object

这是我的代码:

var bulletinApp = angular.module('bulletinApp', ['ngResource']);

bulletinApp.controller('PostsController', ['$scope', 'Post', function($scope, Post) {
    $scope.heading = 'Welcome';
    $scope.posts = Post.query();

    $scope.newPost = {
        title: 'Test Post'
    }

}]);

bulletinApp.factory('Post', ['$resource', function($resource) {
    return $resource('/posts');
}]);

我在这里找到了答案:在 AngularJS/Rails 中使用 $resource.query() 函数时资源配置错误 https://stackoverflow.com/questions/20037852/error-in-resource-configuration-while-using-resource-query-function-with-angu

这就是说我应该将其添加到我的资源声明中:

'query': {method: 'GET', isArray: false }

问题是我不知道应该在哪里添加它,或者即使这是我遇到的问题?

EDIT:

bulletinApp.factory('Post', ['$resource', function($resource) {
  return $resource('/posts', {'query': {method: 'GET', isArray: false}});
}]);

此后我仍然收到错误。同样在教程中,没有必要这样做,而且我已经完全遵循了它,为什么 get 方法会获取对象而不是数组?


Add {} after '/posts'.

bulletinApp.factory('Post', ['$resource', function($resource) { 
    return $resource('/posts', {}, {'query': {method: 'GET', isArray: false}}); 
}]);

详细信息请参见这个帖子 https://stackoverflow.com/questions/20041306/angular-js-how-fix-error-resourcebadcfg-error-in-resource-configuration-e.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐