I have enabled csrfToken
in yii
and it works fine with simple post request. But whenever i have ajax request
it fails I used 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 so i am asking this question. Please help if you can :)