简体   繁体   English

Rails 4和AngularJS 1.3.x的406不可接受错误$ http.get

[英]406 Not Acceptable Error with Rails 4 and AngularJS 1.3.x $http.get

I have looked at other examples on StackOverflow, and I still need help with this. 我在StackOverflow上查看了其他示例,但我仍然需要帮助。

My issue below is with the o.get function to retrieve a single post by its id. 我下面的问题是o.get函数通过其ID检索单个帖子。

While debugging, I have found that I am actually hitting this function when I click the proper link. 在调试时,我发现当我单击正确的链接时实际上正在击打此功能。 However, it is throwing a 406 Not Acceptable error when clicked. 但是,单击时抛出406 Not Acceptable错误。

I am not sure if the issue is on the angular side, or with my show action, which is also found below. 我不确定这个问题是在角度上还是在我的表演动作上,也可以在下面找到。

Here is the code for my service: 这是我的服务代码:

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;
}])

The show action: 表演动作:

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

Any help would be greatly appreciated. 任何帮助将不胜感激。

Thanks! 谢谢!

EDIT: 编辑:

Here is what is in my controllers: 这是我的控制器中的内容:

Here is what I have in my Application Controller, I have the following: 这是我的应用程序控制器中的内容,我具有以下内容:

respond_to :json

Most Controller is as follows: 大多数控制器如下:

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

You need to add respond_to (with list of formats you're using) at the top of your controller to make respond_with work. 您需要在控制器顶部添加respond_to (带有正在使用的格式列表),以使respond_with工作。 You can read more about it here: http://apidock.com/rails/ActionController/MimeResponds/respond_with 您可以在此处了解更多信息: http : //apidock.com/rails/ActionController/MimeResponds/respond_with

class PostsController < ApplicationController

  respond_to :json

  def index
    respond_with Post.all
  end

end

I ran into the same problem. 我遇到了同样的问题。 A work-around that I tried was adding '.json' to the o.get function like so: 我尝试过的一种变通办法是将'.json'添加到o.get函数中,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM