3

I have enabled csrfToken in Yii 1 and it works fine with simple post request. But whenever I have AJAX request it fails. I used the below code:

main.php

'components' => array(
    'request' => array(
        'enableCsrfValidation' => true,
    ),
),

I found it for ajax request solution from one wiki article but this not worked

Controller.php

public function init() {
        parent::init();
        $this->initAjaxCsrfToken();
    }

    // this function will work to post csrf token.
    protected function initAjaxCsrfToken() {

        Yii::app()->clientScript->registerScript('AjaxCsrfToken', ' $.ajaxSetup({
                         data: {"' . Yii::app()->request->csrfTokenName . '": "' . Yii::app()->request->csrfToken . '"},
                         cache:false
                    });', CClientScript::POS_HEAD);
    }

I need a global solution.

halfer
  • 19,824
  • 17
  • 99
  • 186
Passionate Coder
  • 7,154
  • 2
  • 19
  • 44

0 Answers0