Ruby on rails Angular在使用JSON api时显示空数组

Ruby on rails Angular在使用JSON api时显示空数组,ruby-on-rails,json,angularjs,Ruby On Rails,Json,Angularjs,我在angular应用程序中使用Rails JSON API时遇到问题。我在Angular中创建了一个服务,用于查询rails数据库中的索引,代码如下: var app = angular.module('myApp'); app.factory('Comment', function($resource) { return $resource('http://localhost:3000/comments.json', {},{ index: { method: 'GET

我在angular应用程序中使用Rails JSON API时遇到问题。我在Angular中创建了一个服务,用于查询rails数据库中的索引,代码如下:

var app = angular.module('myApp');

app.factory('Comment', function($resource) {
    return $resource('http://localhost:3000/comments.json', {},{
      index: { method: 'GET', isArray: true, responseType: 'json' },
      show: { method: 'GET', responseType: 'json' },
      update: { method: 'PUT', responseType: 'json' }
    });
  });


app.controller('MainCtrl', function ($scope, Comment) {
    $scope.json = Comment.index();
  });
  def index
    @comments = Comment.all

    render json: @comments
  end
我的Rails控制器代码如下所示:

var app = angular.module('myApp');

app.factory('Comment', function($resource) {
    return $resource('http://localhost:3000/comments.json', {},{
      index: { method: 'GET', isArray: true, responseType: 'json' },
      show: { method: 'GET', responseType: 'json' },
      update: { method: 'PUT', responseType: 'json' }
    });
  });


app.controller('MainCtrl', function ($scope, Comment) {
    $scope.json = Comment.index();
  });
  def index
    @comments = Comment.all

    render json: @comments
  end
我的路由文件只是:

resources :comments
我知道我的Angular应用程序正在ping我的rails API以获取信息,因为当我启动服务器时,我的rails控制台会显示:

Started GET "/comments.json" for 127.0.0.1 at 2014-05-29 20:36:16 -0700
Processing by CommentsController#index as JSON
Comment Load (0.2ms)  SELECT "comments".* FROM "comments"
Completed 200 OK in 2ms (Views: 2.1ms | ActiveRecord: 0.2ms)

但当我在HTML文件中绑定
$scope.json
时,它只返回一个空数组。

将url改为“/comments”,而不是“/comments.json”。您的rails应用程序直接响应“/评论”。您也可以通过转到“/comments”和“/comments.json”来检查浏览器,并查看它们返回的内容。同意上述语句,使用POSTMan、curl或Firebug或浏览器(F12)中的网络面板调试网络请求,以了解原始格式的结果,并能够“测试”你的服务层是分开的。实际上我自己解决了这个问题。我更改了路由文件,将资源放在:api名称空间下。我以前尝试过,但后来我将控制器类作为Api模块的一部分,并将控制器文件实际移动到控制器文件夹下的Api文件夹中。这最终将我的json输出正确地公开给我的angular控制器。谢谢你们的回复。