Home >PHP Framework >YII >yii2 How to solve the problem that the data you submitted cannot be verified

yii2 How to solve the problem that the data you submitted cannot be verified

angryTom
angryTomOriginal
2019-11-06 16:01:217826browse

yii2 How to solve the problem that the data you submitted cannot be verified

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;
in the controller

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:{
        &#39;<?php echo \yii\web\Request::CSRF_HEADER; ?>&#39; : &#39;<?php echo Yii::$app -> request -> csrfToken; ?>&#39; // _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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn