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
2110 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表单中加入隐藏域

Copy after login

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

var csrfToken = $('meta[name="csrf-token"]').attr("content");
$.ajax({
    type: 'POST',
    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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!