Yii框架Bad Request (#400) 您提交的数据无法被验证。的解决方案

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

解决办法应该关闭csrf验证。

如何关闭csrf验证表单的功能呢?

在配置文件中关闭,如果是前台操作,打开你的frontend/config/main-local.PHP

components默认的配置为:

'request' => [
    // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
    'cookieValidationKey' => 'ihQ_D17WNrIdA3bv5o3nCIQ_Q5KD7d8J',
],

我们只需要把Yii框架中的CSRF令牌验证关闭即可,修改为:

'request' => [
    // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
    'cookieValidationKey' => 'ihQ_D17WNrIdA3bv5o3nCIQ_Q5KD7d8J',
    "enableCsrfValidation"=>false,
],



猜你喜欢

转载自blog.csdn.net/li_lening/article/details/80640039