Home > database >  rails ajax post invalid authenticity token
rails ajax post invalid authenticity token

Time:09-24

This ajax POST request made from a react component in a rails app returns an invalid authenticity token error Document includes <%= csrf_meta_tags %>

$.ajax({
  type: 'POST',
  url: posturl,
  parameters: {
    authenticity_token: $('[name="csrf-token"]')[0].content,
    msg_time: msg_time,
    sender: sender,
    sender_name: sender_name,
    body: msgbody
  },
  success: function(res){
    console.log(res);
  },
  error: function(err) {
    console.log(err);
  }
});

CodePudding user response:

Change parameters to data as following:


$.ajax({
  type: 'POST',
  url: posturl,
  data: {
    authenticity_token: $('[name="csrf-token"]')[0].content,
    msg_time: msg_time,
    sender: sender,
    sender_name: sender_name,
    body: msgbody
  },
  success: function(res){
    console.log(res);
  },
  error: function(err) {
    console.log(err);
  }
});
  • Related