JQuery Ajax calls in Rails 3 to receive 401 unauthorized request

I have a Rails 3.1 application that uses an authentication program. I am trying to make a call via ajax to a url. For some reason I keep getting 401 unauthorized access. I read here http://henrik.nyh.se/2008/05/rails-authenticity-token-with-jquery on how to authenticate jQuery requests. Request code

        $.get('/chromosomes.json', {sequence_start: '10', sequence_end: '100'},
        function(data){
                        alert(data)
       }, 'json');

      

It is strange that on the server I see

Started GET "/chromosomes.json?sequence_start=10&sequence_end=100&_=1320958293654" for 127.0.0.1 on Thu 10 Nov 15:51:33 -0500 2011 Processing by ChromosomesController index # as JSON Parameters: {"sequence_start" => "10", "_" => "1320958293654", "sequence_end" => "100"} Completed in 10ms

In my controller I:

before_filter :authenticate_user!

def index

        respond_to do |format|
      format.html
      format.json  { render :json => @chromosomes }
    end
end

      

For some reason, the authentication token is not passed to Rails. If I remove authenticate_user! before the filter it works great !? Any help is greatly appreciated. Thank!

+2


source to share


2 answers


I fixed this issue by adding: token_authenticatable to my user model. This allows users to authenticate using session tokens.



+4


source


I am developing with angularjs to send REST requests, so I use the ng-rails-csrf gem to add the csrf token to every ajax request sent for authentication.



0


source







All Articles