You do not need to set cookies manually.
If you use jQuery, the CSRF token will be sent automatically.
For example, for AngularJS, you can add it manually to request the following parameters:
yii.getCsrfParam(): yii.getCsrfToken()
Make sure you have YiiAsset .
Otherwise, you can get them from the meta tags (basically what these two methods do):
$('meta[name=csrf-param]').prop('content'): $('meta[name=csrf-token]').prop('content')
Also note that to enable CSRF checking, the Controller property and Request property enableCsrfValidation must be set to true .
Update:
Another important thing to understand:
CSRF token will be checked only by these methods: GET , HEAD , OPTIONS .
Also make sure the main layout has <?= Html::csrfMetaTags ?> .
arogachev
source share