Yii 错误 400 尝试删除帖子时无法验证 CSRF 令牌

问题描述:

当我尝试删除帖子时,出现此错误:

When I was trying to delete a post I got this error:

Yii Error 400 The CSRF token could not be verified

我不知道究竟是什么导致了这种情况以及可能与什么有关.这是我的操作删除:

I don't know what is exactly causing this and to what it could be related. here is my action delete:

    public function actionDelete($id) {

         if (Yii::app()->request->isPostRequest) {
                // we only allow deletion via POST request
                $this->loadModel($id)->delete();

                // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
                if (!isset($_GET['ajax']))
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
        }
        else
              throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
    }

    protected function afterDelete()
    {
        parent::afterDelete();
        Image::model()->deleteAll('name='.$this->id);
        Date::model()->deleteAll('tbl_show_id='.$this->id);
        Press::model()->deleteAll('tbl_show_id='.$this->id);
    }

您似乎已启用 CSRF 验证.如果您想使用它,请阅读文档并确保在每个 POST 请求中发送 CSRF 令牌.

It seems you've enabled CSRF validation. If you want to use it, read the doc and make sure you send the CSRF token in every POST request.