Home > PHP Framework > YII > body text

What should I do if the yii2 submission form prompts that it cannot be verified?

王林
Release: 2020-02-19 15:39:43
Original
2235 people have browsed it

What should I do if the yii2 submission form prompts that it cannot be verified?

问题:

yii2提交表单提示无法验证。

原因:

之所以提示无法验证是因为对于post请求,是有一个CSRF验证的。

(推荐教程:yii框架

解决方法:

第一种解决办法是关闭Csrf

public function init()
{
    $this->enableCsrfValidation = false;
}
//或者
public function __construct($id, $module, $config = [])
{
    $this->menuActive = 2;
    $this->enableCsrfValidation = false;
    parent::__construct($id, $module, $config);     
          
}
//总之把enableCsrfValidation设为false就可以了
Copy after login

第二种解决办法是在form表单中加入隐藏域

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

第三种解决办法是在AJAX中加入_csrf字段

var csrfToken = $(&#39;meta[name="csrf-token"]&#39;).attr("content");
$.ajax({
    type: &#39;POST&#39;,
    url: url,
    data: {
        _csrf:csrfToken},
        success: success,
        dataType: dataType
});
Copy after login

更多编程相关内容,请关注php中文网编程入门栏目!

The above is the detailed content of What should I do if the yii2 submission form prompts that it 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