【问题标题】:yii2 When delete GridView : Unable to verify your data submissionyii2 删除 GridView 时:无法验证您的数据提交
【发布时间】:2017-02-12 18:54:53
【问题描述】:

当我想删除 GridView 上的项目时,我收到此错误:

exception 'yii\web\BadRequestHttpException' with message 'Unable to verify your data submission.

这是我的控制器代码:

class DevisController extends Controller
{
 public $layout = 'lay-admin';

 public function behaviors()
 {
    return [
        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'delete' => ['post'],
            ],
        ],
    ];
}
/* ..... */
public function actionDelete($id)
{
    $this->findModel($id)->delete();

    return $this->redirect(['index']);
}

当我在行为函数中将 post 方法更改为 get 方法时,我收到此错误

Method Not Allowed. This url can only handle the following request methods: GET.

GridView 代码:

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
                'columns' => [
                    ['class' => 'yii\grid\SerialColumn'],

                    //'idDevis',
                    'reference',
                    'client',
                    'dateCreation',
                    'contact',
                    'valableJusqua',
                    'dateRelance',
                    [
                     'attribute'=>'etat',
                     'filter'=>ArrayHelper::map(Devis::find()->asArray()->all(), 'etat', 'etat'),
                    ],
                    'commercial',
                    'modePaiement',
                    'delaiPaiement',

                    ['class' => 'yii\grid\ActionColumn'],
                ],
]); ?>

请给个意见!!

【问题讨论】:

  • 您的视图中是否存在 CSRF 参数?
  • 我不认为,我看不到这个参数
  • 现在我收到此错误Method Not Allowed. This url can only handle the following request methods: POST.
  • 粘贴有问题的gridview的代码。
  • 好的,我会更新我的问题

标签: gridview yii2 delete-row behavior


【解决方案1】:

在您的自定义布局文件中添加 CSRF 元标记。

例子:

<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>">
<head>
    <meta charset="<?= Yii::$app->charset ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <?= Html::csrfMetaTags() ?>

    <title><?= Html::encode($this->title) ?></title>
    <?php $this->head() ?>
</head>
<body>
<?php $this->beginBody() ?>

Here you can read more about CSRF.

【讨论】:

  • 如果我有 csrf 但删除时仍然得到 bad request 400 怎么办?
猜你喜欢
  • 1970-01-01
  • 2017-06-20
  • 2021-12-30
  • 2014-12-15
  • 2015-10-15
  • 2021-12-04
  • 2015-09-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多