I am sending data from view to controller with AJAXand I got this error:
WARNING: Can\'t verify CSRF token authenticity
I think
<%= csrf_meta_tag %>
in your layoutbeforeSend
to include the csrf-token in the ajax request to set the header. This is only required for post
requests.The code to read the csrf-token is available in the rails/jquery-ujs
, so imho it is easiest to just use that, as follows:
$.ajax({
url: url,
method: 'post',
beforeSend: $.rails.CSRFProtection,
data: {
// ...
}
})