WARNING: Can't verify CSRF token authenticity rails

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

    The top voted answers here are correct but will not work if you are performing cross-domain requests because the session will not be available unless you explicitly tell jQuery to pass the session cookie. Here's how to do that:

    $.ajax({ 
      url: url,
      type: 'POST',
      beforeSend: function(xhr) {
        xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'))
      },
      xhrFields: {
        withCredentials: true
      }
    });
    

提交回复
热议问题