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


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

送信する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);
      });
    };