Home > Article > PHP Framework > yii csrf The data you submitted cannot be verified
Solution:
1. Turn off csrf verification
Add
public $enableCsrfValidation = false;
in the controller 2, Add hidden fields in 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, which means that 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.
Recommended tutorial: yii framework
3. Ajax submission
I use the axios ajax library, but they are all similar.
axios({ url: url, headers:{ '<?php echo \yii\web\Request::CSRF_HEADER; ?>' : '<?php echo Yii::$app -> request -> csrfToken; ?>' // _csrf验证 }, data: postData, }).then(response => { // 请求成功 回调 }).catch(error => { // 请求失败 回调 })
For more programming-related content, please pay attention to the Programming Introduction column on the php Chinese website!
The above is the detailed content of yii csrf The data you submitted cannot be verified. For more information, please follow other related articles on the PHP Chinese website!