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.