首页 > 技术文章 > Yii框架用ajax提交表单时候报错Bad Request (#400): Unable to verify your data submission.

yuancr 2017-07-22 14:38 原文

提交表单报400错误,提示 “您提交的数据无法验证”
原来是csrf验证的问题,因为表单是自己写的,在Yii框架中,为了防止csrf攻击,对post的表单数据封装了CSRF令牌验证。
解决办法关闭csrf验证

frontend/config/main-local.PHP

在配置文件中关闭

$config = [
    'components' => [
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - RG2V-Dz_PXVhz7GUtKtbhjIcZ3JPmXaTthis is required by cookie validation
            'cookieValidationKey' => 'my-first-yii-web-frontend',
            "enableCsrfValidation"=>false,
        ],
    ],
];

只需要加入"enableCsrfValidation"=>false,就可以了

 

推荐阅读