WARNING: Can't verify CSRF token authenticity rails

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

    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'))
      }
    

提交回复
热议问题