1
votes

So, I've enabled the CSRF component in the initialize function of my AppController. But I have a function that gets submitted to via AJAX and sends data from dynamically created inputs that are not inside a form:

$('a#delete').on('click', function(e) {
    e.preventDefault();
    $.post($(this).attr('href'), $('input.delete:checked').serialize());
})

I'm wondering if there is a way to create the CSRF token for dynamically created inputs that are not inside a form?

I've read the documentation regarding the Csrf component, but it doesn't say anything on how to create a Csrf token, or even if it's possible with dynamically created inputs that aren't contained in a form.

1

1 Answers

2
votes

Add the _csrfToken as a form parameter after reading the cookie value:

var csrf = $.cookie('crsf');
$.post($(this).attr('href'), $('input.delete:checked').serialize() + '&_csrfToken=' + csrf);