Yii Ajax request CSRF can not be verified

2019-06-26 06:09发布

问题:

I use a few different AJAX calls in one of my pages through a couple of different methods.

  1. CHtml::link()
  2. CHtml::ajax()
  3. Within CGridview

Since enabling CSRF I'm having difficulty verifying the token. What is the correct way? I've read a few posts, but struggling to implement. For instance in CHtml::link() I've tried:

'data' => "Yii::app()->request->csrfTokenName = Yii::app()->request->csrfToken",

and also within CGridview:

data: {
    Yii::app()->request->csrfTokenName => Yii::app()->request->csrfToken
},

回答1:

1- Fot ajax you have ajax, ajaxLink and ajaxButton not just link.

2- CSRF token works when you use POST request

3- Add CSRF token like this:'data'=>array('YII_CSRF_TOKEN' => Yii::app()->request->csrfToken)`



标签: ajax yii csrf