yii2 The data you submitted cannot be verified. How to solve the problem?
In yii, it is often encountered that you submit The data cannot be verified. This is because yii has a csrf verification.
Turn off csrf verification
a) Add
public $enableCsrfValidation = false;
b) Add a hidden field in the form
<input name="_csrf" type="hidden" id="_csrf" value="<?= Yii::$app -> request -> csrfToken ?>">
However, in the advanced version, there will be a frontend and a backend, which means that the name is not necessarily _csrf, it may be _csrf-backend or _csrf-frontend and so on. The field name of
_csrf must be consistent with the current page
c) ajax submission
I use the axios ajax library, but they are all the same
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: "YII Tutorial"
The above is the detailed content of yii2 How to solve the problem that the data you submitted cannot be verified. For more information, please follow other related articles on the PHP Chinese website!