【问题标题】:406 Not Acceptable Error with Rails 4 and AngularJS 1.3.x $http.getRails 4 和 AngularJS 1.3.x $http.get 出现 406 不可接受的错误
【发布时间】:2015-01-18 18:40:05
【问题描述】:

我查看了 StackOverflow 上的其他示例,但我仍然需要这方面的帮助。

下面我的问题是使用 o.get 函数通过其 id 检索单个帖子。

在调试时,我发现当我点击正确的链接时,我实际上是在点击这个功能。但是,单击时会引发406 Not Acceptable 错误。

我不确定问题是在角度方面,还是与我的表演动作有关,这也可以在下面找到。

这是我的服务的代码:

angular.module('hackerNews')
.factory('posts', ['$http',
    function($http) {
        var o = {
            posts: []
        };

    o.getAll = function() {
        return $http.get('/posts.json').success(function(data) {
            angular.copy(data, o.posts);
        });
    };

    o.create = function(post) {
        return $http.post('/posts.json', post).success(function(data) {
            o.posts.push(data);
        });
    };

    o.upvote = function(post) {
        return $http.put('/posts/' + post.id + '/upvote.json').success(function(data) {
            post.upvotes += 1;
        });
    };

    o.get = function(id) {
        return $http.get('/posts/' + id).then(function(res) {
            return res.data;
        });
    };

    o.addComment = function(id, comment) {
        return $http.post('/posts/' + id + '/comments.json', comment);
    };

    o.upvoteComment = function(post, comment) {
        return $http.put('/posts/' + post.id + '/comments' + comment.id + '/upvote.json')
        .success(function(data) {
            comment.upvotes += 1;
        });
    };
    return o;
}])

表演动作:

def show
    respond_with Post.find(params[:id])
end

任何帮助将不胜感激。

谢谢!

编辑:

这是我的控制器中的内容:

这是我的应用程序控制器中的内容,我有以下内容:

respond_to :json

大部分控制器如下:

class PostsController < ApplicationController

    def index
        respond_with Post.all
    end

    def create
        respond_with Post.create(post_params)
    end

    def show
        respond_with Post.find(params[:id])
    end

    def upvote
        post = Post.find(params[:id])
        post.increment!(:upvotes)

        respond_with post
    end 

    private

    def post_params
        params.require(:post).permit(:link, :title)
    end
end

【问题讨论】:

  • 抱歉,我指的是 Angular 1.3.x,而不是 13.x
  • 您是否使用 JSON 响应? 406 错误表示服务器的响应类型不在您请求的接受类型列表中。
  • 你的控制器中有respond_to :json吗?
  • 我用控制器中的内容更新了问题。
  • @basia。线程更新。我应该把respond_to :json 代码放在什么地方,在post 控制器的底部?

标签: javascript ruby-on-rails angularjs rest


【解决方案1】:

您需要在控制器顶部添加respond_to(以及您正在使用的格式列表)以使respond_with 工作。你可以在这里阅读更多信息:http://apidock.com/rails/ActionController/MimeResponds/respond_with

class PostsController < ApplicationController

  respond_to :json

  def index
    respond_with Post.all
  end

end

【讨论】:

  • 我在控制器顶部添加了 respond_to :html, :xml, :json。现在我收到 500 错误。
【解决方案2】:

我遇到了同样的问题。我尝试的一种解决方法是将“.json”添加到 o.get 函数中,如下所示:

o.get = function(id) {
  return $http.get('/posts/' + id + '.json').then(function(res) {
    return res.data;
  });
};

【讨论】:

  • 你能解释一下为什么这会解决 OP 的问题吗?
  • 我只是认为页面可能没有加载,因为当控制器中的响应应该是 JSON 时,PostsController#show 操作试图将请求处理为 HTML。不过,这可能不是真正的解决方案
猜你喜欢
  • 1970-01-01
  • 2013-12-18
  • 1970-01-01
  • 2015-03-17
  • 2017-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-07
相关资源
最近更新 更多