The Csrf token could not be verified?

时间:2016-11-29 10:00:25

标签: php ajax yii csrf

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 :)

0 个答案:

没有答案