【发布时间】:2016-01-30 06:48:23
【问题描述】:
我正在开发 Rails + AngularJS 应用程序。为了访问我的应用程序的 json 响应,我必须在我的角度 $resource url 中使用 /something.json 而不仅仅是 /something
有办法解决吗?
路线
upvote_post_comment PUT /posts/:post_id/comments/:id/upvote(.:format) comments#upvote
post_comments POST /posts/:post_id/comments(.:format) comments#create
post_comment GET /posts/:post_id/comments/:id(.:format) comments#show
upvote_post PUT /posts/:id/upvote(.:format) posts#upvote
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
post GET /posts/:id(.:format) posts#show
GET / home#index
PostsController
class PostsController < ApplicationController
def index
@posts = Post.all
respond_to do |format|
format.json { render json: @posts }
end
end
end
Angular 服务提供者
angular.module('flapperNews')
.factory('posts', ['$resource', function($resource) {
return $resource('/posts.json/:id', {id: "@id"})
}]);
【问题讨论】:
-
我认为您可以简单地将 + '.json' 添加到第一个参数
-
我不相信他想要在他的 Angular 应用程序中添加
.json扩展? -
它不起作用, .json 在 url 中意味着您正在访问的资源不是将内容类型返回为 json ,而是返回一个 html。所以尝试将你服务器的内容类型设置为“application/json”然后看看。
标签: javascript ruby-on-rails json angularjs routes