I need to extend all my ajax posts with a csrf token from cookies. I've tried following:
$.ajaxPrefilter(function(options, originalOptions, jqXHR) {
if(options.type.toUpperCase() === "POST")
options.data = options.data || {};
$.extend(options.data, { csrf: $.cookie('cookiename') });
});
But it doesn seem to get applied to any of my existing posts. My console returns:
Blocking Post Request: no csrf token parameter
If i console.log following:
console.log($.cookie('CSRF'), 'waah');
It logs fine. Any ideas?
That's because options.data
is not object, it's query string and that's why you can't merge two objects.
You should to append parameters to query string, something like this:
$.ajaxPrefilter(function(options, originalOptions, jqXHR) {
if(options.type.toUpperCase() === "POST") {
options.data += options.data ? '&' : '';
options.data += 'csrf=' + $.cookie('CSRF');
}
});
You can change the following line :
$.extend(options.data, { csrf: $.cookie('cookiename') });
By this one :
options.data = $.param( $.extend(originalOptions.data, { csrf: $.cookie('cookiename') }) );
Note : By default options.data
is a String
and not an Object
instead of originalOptions.data
.
Hope this helps.
Firebase Cloud Functions: PubSub, "res.on is not a function"
TypeError: Cannot read properties of undefined (reading 'createMessageComponentCollector')
There are several li elements with a data-id attribute like
I would like counting the total number of element on my page, after which the new element has been loaded
I'm having problems with passing two arrays of strings as arguments in JSON format to invoke ASMX Web Service method via jQuery's "POST"