WARNING: Can't verify CSRF token authenticity rails

后端 未结 17 1002
生来不讨喜
生来不讨喜 2020-11-22 06:05

I am sending data from view to controller with AJAXand I got this error:

WARNING: Can\'t verify CSRF token authenticity

I think

相关标签:
17条回答
  • 2020-11-22 06:26

    Use jquery.csrf (https://github.com/swordray/jquery.csrf).

    • Rails 5.1 or later

      $ yarn add jquery.csrf
      
      //= require jquery.csrf
      
    • Rails 5.0 or before

      source 'https://rails-assets.org' do
        gem 'rails-assets-jquery.csrf'
      end
      
      //= require jquery.csrf
      
    • Source code

      (function($) {
        $(document).ajaxSend(function(e, xhr, options) {
          var token = $('meta[name="csrf-token"]').attr('content');
          if (token) xhr.setRequestHeader('X-CSRF-Token', token);
        });
      })(jQuery);
      

    0 讨论(0)
  • 2020-11-22 06:29

    For those of you that do need a non jQuery answer you can simple add the following:

    xmlhttp.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'));
    

    A very simple example can be sen here:

    xmlhttp.open("POST","example.html",true);
    xmlhttp.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'));
    xmlhttp.send();
    
    0 讨论(0)
  • 2020-11-22 06:30

    if someone needs help related with Uploadify and Rails 3.2 (like me when I googled this post), this sample app may be helpful: https://github.com/n0ne/Uploadify-Carrierwave-Rails-3.2.3/blob/master/app/views/pictures/index.html.erb

    also check the controller solution in this app

    0 讨论(0)
  • 2020-11-22 06:32

    Indeed simplest way. Don't bother with changing the headers.

    Make sure you have:

    <%= csrf_meta_tag %> in your layouts/application.html.erb
    

    Just do a hidden input field like so:

    <input name="authenticity_token" 
                   type="hidden" 
                   value="<%= form_authenticity_token %>"/>
    

    Or if you want a jQuery ajax post:

    $.ajax({     
        type: 'POST',
        url: "<%= someregistration_path %>",
        data: { "firstname": "text_data_1", "last_name": "text_data2", "authenticity_token": "<%= form_authenticity_token %>" },                                                                                  
        error: function( xhr ){ 
          alert("ERROR ON SUBMIT");
        },
        success: function( data ){ 
          //data response can contain what we want here...
          console.log("SUCCESS, data="+data);
        }
    });
    
    0 讨论(0)
  • 2020-11-22 06:33

    I'm using Rails 4.2.4 and couldn't work out why I was getting:

    Can't verify CSRF token authenticity
    

    I have in the layout:

    <%= csrf_meta_tags %>
    

    In the controller:

    protect_from_forgery with: :exception
    

    Invoking tcpdump -A -s 999 -i lo port 3000 was showing the header being set ( despite not needing to set the headers with ajaxSetup - it was done already):

    X-CSRF-Token: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    Content-Type: application/x-www-form-urlencoded; charset=UTF-8
    X-Requested-With: XMLHttpRequest
    DNT: 1
    Content-Length: 125
    authenticity_token=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    

    In the end it was failing because I had cookies switched off. CSRF doesn't work without cookies being enabled, so this is another possible cause if you're seeing this error.

    0 讨论(0)
提交回复
热议问题