yii2框架中表单提交出现Bad Request (#400)---Unable to verify your data submission的错误解决方
2016-11-29 21:20
609 查看
提交表单报400错误,提示 “您提交的数据无法验证”
原来是csrf验证的问题,因为表单是自己写的,在Yii框架中,为了防止csrf攻击,对post的表单数据封装了CSRF令牌验证。
解决办法关闭csrf验证
frontend/config/main-local.PHP中
方法一、在配置文件中关闭
使用Yii表单生成页面的时候,如果表单的提交方式为POST,是都会在页面中添加一个隐藏字段,这个隐藏字段就是CSRF令牌验证字段
用户在提交表单的同时,将该字段提交给服务器端,Yii框架会将该有客户端提交过来的隐藏字段和客户端提交过来的Cookie中的YII_CSRF_TOKEN值进行比较。
相同则通过继续执行,不相同则会抛出400异常:"The CSRF token could not be verified."。
所以就出现了上面的问题,如果是自己写的表单,可以在视图页面的表单添加隐藏的令牌验证字段
方法二、在表单中增加隐藏的验证字段
如果不想csrf验证就将方法一的true修改为false,这样YII就不会做post表单验证处理。
原来是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) - this is required by cookie validation 'cookieValidationKey' => 'C86SfpluqpUwqzEp4MSl_mchb2Wr6ris', "enableCsrfValidation"=>false, ], ], ];
使用Yii表单生成页面的时候,如果表单的提交方式为POST,是都会在页面中添加一个隐藏字段,这个隐藏字段就是CSRF令牌验证字段
用户在提交表单的同时,将该字段提交给服务器端,Yii框架会将该有客户端提交过来的隐藏字段和客户端提交过来的Cookie中的YII_CSRF_TOKEN值进行比较。
相同则通过继续执行,不相同则会抛出400异常:"The CSRF token could not be verified."。
所以就出现了上面的问题,如果是自己写的表单,可以在视图页面的表单添加隐藏的令牌验证字段
方法二、在表单中增加隐藏的验证字段
<input name="_csrf" type="hidden" id="_csrf" value="<?= Yii::$app->request->csrfToken ?>">
如果不想csrf验证就将方法一的true修改为false,这样YII就不会做post表单验证处理。
相关文章推荐
- YYII框架中表单提交出现Bad Request (#400)---Unable to verify your data submission的错误解决方法
- yii POST提交 Bad Request (#400)---Unable to verify your data submission的错误解决方法
- yii2 Bad Request (#400)---Unable to verify your data submission
- yii 2 提交表单报错:Unable to verify your data submission.加入了_csrf也报400错误的解决
- yii2 Bad Request (#400)---Unable to verify your data submission
- Bad Request(#400) Unable to verify your data submission
- Yii2表单提交时报错:Unable to verify your data submission
- Unable to verify your data submission.加入了_csrf也报400错误的解决
- [Yii2]Unable to verify your data submission(你提交的资料无法被验证)
- [YII2]Unable to verify your data submission
- Yii-Unable to verify your data submission 错误(CSRF)
- yii2 Unable to verify your data submission
- YII2 Unable to verify your data submission.
- yii2 Unable to verify your data submission
- Yii2 Unable to verify your data submission.
- SharePoint designer 2010编辑List的表单时出现错误:soap:Server was unable to process request. ---> Value does not fall within the expected range
- Android studio出现Error:Unable to tunnel through proxy. Proxy returns "HTTP/1.1 400 Bad Request"的解决办法
- Android studio出现Error:Unable to tunnel through proxy. Proxy returns "HTTP/1.1 400 Bad Request"的解决办法
- Android studio出现Error:Unable to tunnel through proxy. Proxy returns "HTTP/1.1 400 Bad Request"的解决办法
- Android studio出现 Error:Unable to tunnel through proxy. Proxyreturns "HTTP/1.1 400 Bad Request"