Home  >  Article  >  PHP Framework  >  In yii2.0, it prompts csrf that the data you submitted cannot be verified.

In yii2.0, it prompts csrf that the data you submitted cannot be verified.

王林
王林Original
2019-12-13 16:50:413348browse

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:{
        &#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 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!

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