2018-07-12 11:50:47 +02:00
|
|
|
import jQuery from 'jquery';
|
|
|
|
|
2018-08-21 11:31:42 +02:00
|
|
|
// rails-ujs installs CSRFProtection for its own ajax implementation. We might need
|
|
|
|
// CSRFProtection for jQuery initiated requests. This code is from jquery-ujs.
|
|
|
|
jQuery.ajaxPrefilter((options, originalOptions, xhr) => {
|
|
|
|
if (!options.crossDomain) {
|
|
|
|
CSRFProtection(xhr);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function csrfToken() {
|
|
|
|
return jQuery('meta[name=csrf-token]').attr('content');
|
|
|
|
}
|
|
|
|
|
|
|
|
function CSRFProtection(xhr) {
|
|
|
|
let token = csrfToken();
|
|
|
|
if (token) {
|
|
|
|
xhr.setRequestHeader('X-CSRF-Token', token);
|
|
|
|
}
|
|
|
|
}
|