[英]Can't find CSRF token as i try to send POST data thru a link in Yii
for the shopping cart i add 'add to cart' link. 对于购物车,我添加了“添加到购物车”链接。 Through adding a 'click' event i make it a POST request. 通过添加一个'click'事件,我将其设为POST请求。 The system replies: 'Can't find the csrf token'; 系统回复:'找不到csrf令牌'; Here is the code: 这是代码:
'add to cart' => array(
'url'=>'Yii::app()->controller->createUrl("assortment/addtocart", array("id"=>$data[id], "YII_CSRF_TOKEN" => ' . Yii::app()->request->csrfToken. ', "csrf"=>' . Yii::app()->request->csrfToken . ' ))',
'click'=>"function(){
$.fn.yiiGridView.update('assortment-grid', {
type:'POST',
url:$(this).attr('href'),
// data:{YII_CSRF_TOKEN: " . Yii::app()->request->csrfToken . "},
/*success:function(data) {
$.fn.yiiGridView.update('assortment-grid');
}*/
})
return false;
}
",
),
I've added the 'csrf' parameter into the link. 我已经在链接中添加了“ csrf”参数。 When i uncomment data:{YII_CSRF_TOKEN: " . Yii::app()->request->csrfToken . "},
the POST request becomes simple GET redirect (csrf validated). 当我取消注释data:{YII_CSRF_TOKEN: " . Yii::app()->request->csrfToken . "},
POST请求将变为简单的GET重定向(已通过csrf验证)。 I do not need to update grid, so i've commented out success:function(data)
. 我不需要更新网格,所以我已经注释掉了success:function(data)
。 Since there should not be redirection, i need to keep the request POST and ajax. 由于不应该进行重定向,因此我需要保留请求POST和ajax。 You might look at screenshots of the POST form: 您可能会看到POST表单的屏幕截图:
How to fix it? 如何解决? Can i pass csrf as a POST parameter? 我可以将csrf作为POST参数传递吗? Can i add a POST submit button into CGridView? 我可以在CGridView中添加POST提交按钮吗?
make sure that you have this code in config 确保您在配置中有此代码
return array(
'components'=>array(
'request'=>array(
'enableCsrfValidation'=>true,
),
),
);
add token as hidden input(if needed): 添加令牌作为隐藏输入(如果需要):
$request = Yii:app()->getRequest();
echo CHtml::textField($request->csrfTokenName, $request->getCsrfToken());
and send all form data 并发送所有表格数据
data: $('#formId').serialize()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.