Home > Article > PHP Framework > In yii2.0, it prompts csrf that the data you submitted cannot be verified.
In yii, we often encounter the situation that "the data you submitted cannot be verified". This is because yii has a csrf verification.
Just turn off csrf verification.
1. Add
public $enableCsrfValidation = false;
to the controller 2. Add a hidden field to the form
<input name="_csrf" type="hidden" id="_csrf" value="<?= Yii::$app -> request -> csrfToken ?>">
However, in the advanced version, there will be front and backends, that is The name is not necessarily _csrf, it may be _csrf-backend or _csrf-frontend, etc. The field name of
_csrf must be consistent with the current page.
3. Ajax submission
The ajax library used here is axios ajax
axios({ url: url, headers:{ '<?php echo \yii\web\Request::CSRF_HEADER; ?>' : ' <?php echo Yii::$app -> request -> csrfToken; ?>' // _csrf验证 }, data: postData, }).then(response => { // 请求成功 回调 }).catch(error => { // 请求失败 回调 })
Recommended related article tutorials: yii tutorial
The above is the detailed content of In yii2.0, it prompts csrf that the data you submitted cannot be verified.. For more information, please follow other related articles on the PHP Chinese website!