Search code examples
ajaxcgridviewyii1.x

Yii1: CGridView link ajax request shows 400 Bad Request


I am working on Yii 1 application. In my application, there is a CGridView where there is a link, which also fires an ajax request on onclick event. I am sending id as parameter. But the ajax return 400 Bad Request error. Please help me in this matter.

Here is the Gridview:

<h3>Civil Cases</h3>
<?php  $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'notifications-grid',
    'dataProvider'=>$dataProvider_civil,
    'summaryText' => false,

    'columns'=>array(
        array(
            'name' => 'case_id',
            'type' => 'raw',
            'value' => 'CHtml::link(CHtml::encode($data->case_id),array("civilcases/view","id"=>$data->case_id), array("onclick"=>"js:readNotification($data->id)"))'
        ),
        array(
            'name' => 'caseno',
            'type' => 'raw',
            'value' => 'CHtml::link(CHtml::encode($data->caseno),array("civilcases/view","id"=>$data->case_id), array("onclick"=>"js:readNotification($data->id)"))'
        ),
        'date_filing',
        'next_date',
        'panel_lawyer_id',

    ),
));
?>

here is the script:

<script>
    var readNotification = function(id) {
        console.log("button clicked with ID: "+id); //getting id here
        $.ajax({
            type:'POST',
            url:'<?php echo Yii::app()->createUrl("notifications/readNotification");?>',
            data: {id: id}
        });
    };
</script>

here is the controller:

public function actionReadNotification(){
    echo $_POST['id'];
}

added readNotification function to the accessRules. When clicking on the link new page is loading but ajax request shows error.


Solution

  • Try adding the csrf token inside your data with your ajax request.

    <script>
        var readNotification = function(id) {
            console.log("button clicked with ID: "+id); //getting id here
            $.ajax({
                type:'POST',
                url:'<?php echo Yii::app()->createUrl("notifications/readNotification");?>',
                data: {id: id,<?= 
                Yii::app()->request->csrfTokenName?>:<?=Yii::app()->request->csrfToken?>,}
            });
        };
    </script>
    

    You can also disable the csrftoken by adding the below in the beforeAction()

    public function beforeAction($action) {
    
        if($action->id=='readnotification'){
            $this->enableCsrfValidation = false;
        }
        return parent::beforeAction($action);
    }
    

    but this is not recommended way of doing the work.

    EDIT

    i mistakenly added Yii::$app->request instead of Yii::app()->request as the first one is for Yii2 and not for Yii1 please change it to

    <?=Yii::app()->request->csrfTokenName?>:<?=Yii::app()->request->csrfToken?>
    

    and make sure you have the request component with the following configurations

    'components'=>array(
    .
    .
    .
        'request'=>array(
              'enableCookieValidation'=>true,
              'enableCsrfValidation'=>true,
              'csrfTokenName'=>'_my-token',
        ),
    

    Note : you can change the _my-token to any other name you like