yii ajax令牌,Use ajax/json in yii framework

You must not hardcode url's. You must use createUrl() or createAbsoluteUrl() as @Grimv01k recommends you. When in view or in controller you can do this by

$this->createUrl( 'foo/bar',array('id'=>321) )

Other ways:

Yii::app()->controller->createUrl('/baz/foo/bar'); // will use current controller

or

Yii::app()->createUrl('/baz/foo/bar')

Edited

So if you want

to unset a session when a user clicks on a icon

you can do this in following way.

echo CHtml::ajaxLink(

'click me to destroy session, mua-ha-ha',

Yii::app()->createUrl( '/session/destroy' ),

array(

'type' => 'post',

'dataType' => 'json',

'data' => array(

'foo' => 'bar',

),

'beforeSend' => 'js:function(){

console.log( $(this) );

return false;

}',

'success' => 'js:function(data){

console.log(data);

}',

)

);

in SessionController:

public function actionDestroy () {

if ( isset( $_POST['foo'] ) ) {

Yii::app()->session->destroy();

if ( Yii::app()->request->isAjaxRequest ) {

$response = array(

'status' => !Yii::app()->session->sessionID

) ;

echo CJSON::encode( (object) $response );

Yii::app()->end();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值