duanbushi1479 2015-07-25 03:12
浏览 188

yii2 - 如何解决错误请求(#400)无法验证您的数据提交?

I have a controller generated by Gii. I modify the behaviors to be like this:

public function behaviors()
{
    return [
        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'delete' => ['post'],
            ],
        ],
        'access' => [
            'class' => AccessControl::className(),
            'rules' => [
                [
                    'allow' => true,
                    'roles' => ['@'],
                ],
            ],
        ],
    ];
}

When I fill the form and submit it, sometimes I get an error

Bad Request (#400) Unable to verify your data submission

But if I click on back button in my browser and re-submit again the form with the same value it will be okay an submitted successfully.

I have been searching in google and stackoverflow, many of them say that the problem is on CSRF Token. But in my layout I've put <?= Html::csrfMetaTags() ?> and in my form there is <input type="hidden" name="_csrf" value="...">

Anyone here can help me to solve this? And explain this why does it happen?

  • 写回答

2条回答 默认 最新

  • duanhong1985 2016-09-01 08:53
    关注

    add in form in view

    <input type="hidden" name="_csrf" value="<?=Yii::$app->request->getCsrfToken()?>" />
    
    评论

报告相同问题?

悬赏问题

  • ¥15 luckysheet
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误