Home  >  Article  >  PHP Framework  >  yii csrf The data you submitted cannot be verified

yii csrf The data you submitted cannot be verified

王林
王林Original
2020-02-20 14:35:591926browse

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

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