问题描述
我有一个使用设计进行验证的Rails 3.1应用程序。我试图让通过AJAX调用的URL。出于某种原因,我不断收到401未经授权的访问。我按照这里的教程 http://henrik.nyh.se / 2008/05 / Rails的真实性令牌与 - jQuery的关于如何进行身份验证的jQuery请求。在code的请求
I have a Rails 3.1 application that uses devise for authentication. I am trying to make a call via ajax to a url. For some reason I keep getting 401 Unauthorized access. I have followed the tutorial here http://henrik.nyh.se/2008/05/rails-authenticity-token-with-jquery on how to authenticate jQuery requests. The code for the request is
$.get('/chromosomes.json', {sequence_start: '10', sequence_end: '100'},
function(data){
alert(data)
}, 'json');
什么是奇怪的是,在服务器上我看到
What is strange is that on the server I see
入门GET/chromosomes.json?sequence_start=10&sequence_end=100&_=1320958293654为127.0.0.1,在周四11月10日15时51分33秒-0500 2011 处理由ChromosomesController#指数为JSON 参数:{sequence_start=>10,_=>1320958293654,sequence_end=>100}在完成10毫秒
Started GET "/chromosomes.json?sequence_start=10&sequence_end=100&_=1320958293654" for 127.0.0.1 at Thu Nov 10 15:51:33 -0500 2011 Processing by ChromosomesController#index as JSON Parameters: {"sequence_start"=>"10", "_"=>"1320958293654", "sequence_end"=>"100"}Completed in 10ms
在我的控制器我有:
before_filter :authenticate_user!
def index
respond_to do |format|
format.html
format.json { render :json => @chromosomes }
end
end
由于某些原因的真实性会话令牌没有被传递到Rails的。如果我删除的authenticate_user!过滤器之前它工作正常!?任何帮助是非常AP preciated。谢谢!
For some reason the authenticity session token is not being passed to Rails. If I remove the authenticate_user! before filter it works fine!? Any help is very much appreciated. Thanks!
推荐答案
我加入解决了这个问题。这允许用户通过会话令牌进行验证。
I fixed this issue by adding :token_authenticatable to my user model. This allows users to be authenticated by session tokens.
这篇关于在Rails 3中得到401未经授权的请求jQuery的Ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!