webpack jquery ajax rails 5 InvalidAuthenticityToken

Viewed 3311

I use Rails 5.1 + webpack When I include jQuery to my project with

 plugins: [
    new webpack.ProvidePlugin({
        'jQuery': 'jquery',
        '$': 'jquery',
        'global.jQuery': 'jquery'
    })
  ]

And try to use jQuery.ajax PUT, POST, or DELETE requests e.g.

var that = this;
$.ajax({
  url: navigator_item.url,
  method: 'DELETE',
  success: function(res) {
   ...
  }
});

I get ActionController::InvalidAuthenticityToken.

But, when I include jQuery with next line in app.js file

import $ from 'jquery'
window.jQuery = $;
window.$ = $; // lazy fix if you want to use jQuery in your inline scripts.

and this in shared.js

module.exports = {
  resolve: {
    alias: {
        'jquery': 'jquery/src/jquery'
    }
  }

jQuery.ajax works fine.

Is there a way using jQuery.ajax when you include jQuery as a plugin?

1 Answers
Related