沃梦达 / 编程问答 / php问题 / 正文

HttpException:400 无法验证您提交的数据

HttpException:400 Unable to verify your data submission(HttpException:400 无法验证您提交的数据)

本文介绍了HttpException:400 无法验证您提交的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的日志文件充满了这些错误

My log files are filled with these errors

2021-11-19 12:39:42 [27.xxx.xxx.xxx][1958][gi96uqh6atadlbsg2ksjfltd9e][error][yiiwebHttpException:400] yiiwebBadRequestHttpException: Unable to verify your data submission. in /var/www/html/vendor/yiisoft/yii2/web/Controller.php:218

似乎无法弄清楚原因.因为我也无法复制这个问题.

Can't seem to figure out why. As i can't replicate the problem either.

我阅读此内容,但该解决方案不适用于我,因为我所有的表单都是使用 $form = ActiveForm::begin([]) 创建的,而且我没有上传文件.

I've read this, but the solution doesn't apply to me as all my forms are created using $form = ActiveForm::begin([]) and i'm not uploading files.

在我的

我有这个

<meta name="csrf-param" content="_csrf-frontend">
<meta name="csrf-token" content="oidpfJVSR28kMxgD4loRdgIs3TCRVITuR6Ly3Z587nLxdgIt-h8XIlFbSECzCEgHUmqaQ9InwaIYzJ2u-ySaIw==">

并且因为我使用 $form = ActiveForm::begin([]),所以我的表单有这个隐藏字段

and because i use $form = ActiveForm::begin([]), for my form there is this hidden field

<form id="form-small" action="/frontend/web/search/" method="post">
<input type="hidden" name="_csrf-frontend" value="oidpfJVSR28kMxgD4loRdgIs3TCRVITuR6Ly3Z587nLxdgIt-h8XIlFbSECzCEgHUmqaQ9InwaIYzJ2u-ySaIw==">

这是我的 $form = ActiveForm::begin([])

$form = ActiveForm::begin([
        'id' => $model->formName().'-form-small',
        'action' => ['/search/default/id'],
        'method' => 'post',
        'scrollToError' => false,
        'validateOnChange' => false,
        'validateOnSubmit' => true,
        'enableClientValidation' => true,
        'enableAjaxValidation' => true,
        'fieldConfig' => [
            'options' => [
                'tag' => false,
            ],
        ],
    ]);

我的页脚中也有这个 JS.(main.php 布局)

i also have this JS in my footer. (main.php layout)

<script>
    $.ajaxSetup({
      headers: {
        'X-CSRF-TOKEN': '<?= yii::$app->request->csrfToken ?>'
      }
    });
</script>

而且我也不想关闭 CSRF.

And i DO NOT want to turn CSRF off either.

在我的 main.php 我有这个

 'request' => [
            'csrfParam' => '_csrf-frontend',
            'enableCsrfCookie' => false, 
            'enableCookieValidation' => true,
            'cookieValidationKey' => 'frontend-cookie-2021',
        ],

我编辑了yii/framework/.php

并在 841

echo ' -- start--';
        echo '<br><br><br>';
        print_r($trueToken); 
        echo '<br><br><br>';
print_r($this->getBodyParam($this->csrfParam));
echo '<br><br><br>';
print_r($this->getCsrfTokenFromHeader());
echo '<br><br><br>'; 
echo ' -- end--';
die;

每次我提交表单时.$trueToken 令牌似乎不同.即使我不刷新页面.

every time i submit my form. the $trueToken token seems to be different. even when i don't refresh the page.

如下图

-- start--
S-r869794GPYBi8voh-dXVDFLLWl8GvWhw6Qvn4c7icYu5e6sbCwLq1uf2zzTcQsAINrxuaDLprYYP_NG0Sadg==

b4GMJgf6dmn8H64oljr6uxokFC2WbQeLP4bY_SI-7Pg80Od3aLcmJIl3_mvHaKPKSmJTXtUeQsdg6LeOR2aYqQ==

b4GMJgf6dmn8H64oljr6uxokFC2WbQeLP4bY_SI-7Pg80Od3aLcmJIl3_mvHaKPKSmJTXtUeQsdg6LeOR2aYqQ==
-- end--

知道如何解决这个问题吗?谢谢.

Any idea how to fix this? Thank you.

推荐答案

一个 CSRF 令牌头名称是 X-CSRF-Token

A CSRF token header name is X-CSRF-Token

https://github.com/yiisoft/yii2/blob/552593ca3bcd9b4c9b19913e9e56de9548db59e3/framework/web/Request.php#L103

获取已经生成的令牌

$.ajaxSetup({
  headers: {
    'X-CSRF-Token': $('meta[name="_csrf-frontend"]').attr('content')
  }
});

这篇关于HttpException:400 无法验证您提交的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:HttpException:400 无法验证您提交的数据

基础教程推荐