WARNING: Can't verify CSRF token authenticity rails

后端 未结 17 1003
生来不讨喜
生来不讨喜 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:18

    Ugrading from an older app to rails 3.1, including the csrf meta tag is still not solving it. On the rubyonrails.org blog, they give some upgrade tips, and specifically this line of jquery which should go in the head section of your layout:

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

    taken from this blog post: http://weblog.rubyonrails.org/2011/2/8/csrf-protection-bypass-in-ruby-on-rails.

    In my case, the session was being reset upon each ajax request. Adding the above code solved that issue.

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

    I just thought I'd link this here as the article has most of the answer you're looking for and it's also very interesting

    http://www.kalzumeus.com/2011/11/17/i-saw-an-extremely-subtle-bug-today-and-i-just-have-to-tell-someone/

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

    If I remember correctly, you have to add the following code to your form, to get rid of this problem:

    <%= token_tag(nil) %>
    

    Don't forget the parameter.

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

    oops..

    I missed the following line in my application.js

    //= require jquery_ujs
    

    I replaced it and its working..

    ======= UPDATED =========

    After 5 years, I am back with Same error, now I have brand new Rails 5.1.6, and I found this post again. Just like circle of life.

    Now what was the issue is: Rails 5.1 removed support for jquery and jquery_ujs by default, and added

    //= require rails-ujs in application.js
    

    It does the following things:

    1. force confirmation dialogs for various actions;
    2. make non-GET requests from hyperlinks;
    3. make forms or hyperlinks submit data asynchronously with Ajax;
    4. have submit buttons become automatically disabled on form submit to prevent double-clicking. (from: https://github.com/rails/rails-ujs/tree/master)

    But why is it not including the csrf token for ajax request? If anyone know about this in detail just comment me. I appreciate that.

    Anyway I added the following in my custom js file to make it work (Thanks for other answers to help me reach this code):

    $( document ).ready(function() {
      $.ajaxSetup({
        headers: {
          'X-CSRF-Token': Rails.csrfToken()
        }
      });
      ----
      ----
    });
    
    0 讨论(0)
  • 2020-11-22 06:25

    If you're using javascript with jQuery to generate the token in your form, this works:

    <input name="authenticity_token" 
           type="hidden" 
           value="<%= $('meta[name=csrf-token]').attr('content') %>" />
    

    Obviously, you need to have the <%= csrf_meta_tag %> in your Ruby layout.

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

    You can write it globally like below.

    Normal JS:

    $(function(){
    
        $('#loader').hide()
        $(document).ajaxStart(function() {
            $('#loader').show();
        })
        $(document).ajaxError(function() {
            alert("Something went wrong...")
            $('#loader').hide();
        })
        $(document).ajaxStop(function() {
            $('#loader').hide();
        });
        $.ajaxSetup({
            beforeSend: function(xhr) {xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'))}
        });
    });
    

    Coffee Script:

      $('#loader').hide()
      $(document).ajaxStart ->
        $('#loader').show()
    
      $(document).ajaxError ->
        alert("Something went wrong...")
        $('#loader').hide()
    
      $(document).ajaxStop ->
        $('#loader').hide()
    
      $.ajaxSetup {
        beforeSend: (xhr) ->
          xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'))
      }
    
    0 讨论(0)
提交回复
热议问题