In Cakephp3 when the Csrf component is enabled. How I can use it in ajax call.
In this beforeSend
parameter of ajax csrf token is set in header. What is the val
The CSRF component writes the current token to the request parameters as _csrfToken
, you can get it via the request object's param()
method (or getParam()
as of CakePHP 3.4):
beforeSend: function(xhr){
xhr.setRequestHeader(
'X-CSRF-Token',
= json_encode($this->request->param('_csrfToken')); ?>
);
},
To make the token available to all your scripts, you can for example make it globally available as variable in your layout template: