WARNING: Can't verify CSRF token authenticity rails

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

    The best way to do this is actually just use <%= form_authenticity_token.to_s %> to print out the token directly in your rails code. You dont need to use javascript to search the dom for the csrf token as other posts mention. just add the headers option as below;

    $.ajax({
      type: 'post',
      data: $(this).sortable('serialize'),
      headers: {
        'X-CSRF-Token': '<%= form_authenticity_token.to_s %>'
      },
      complete: function(request){},
      url: "<%= sort_widget_images_path(@widget) %>"
    })
    

提交回复
热议问题