LoginSignup
0
1

More than 3 years have passed since last update.

Ajax通信でActionController::InvalidAuthenticityTokenしたときの対応

Posted at

原因とか理由とかすっ飛ばして結論

送信するdataにauthenticity_token: $('meta[name="csrf-token"]').attr('content')}を追加。

    let ajax = function (method, data) {
      data = $.extend(data, {authenticity_token: $('meta[name="csrf-token"]').attr('content')});
      $.ajax({
        type: method,
        url: "hope/the/colona/is/orver",
        timeout: 3000,
        data: data,
        dataType: 'json'
      }).done(function (response, textStatus, jqXHR) {
        console.log(response);
      }).fail(function (jqXHR, textStatus, errorThrown) {
        console.log(response);
      });
    };
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1