Home > PHP Framework > YII > body text

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

王林
Release: 2019-12-13 16:50:41
Original
3356 people have browsed it

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;
Copy after login

to the controller 2. Add a hidden field to the form

<input name="_csrf"
       type="hidden"
       id="_csrf"
       value="<?= Yii::$app -> request -> csrfToken ?>">
Copy after login

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 => {
    // 请求失败 回调
})
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template