Untuk membuat link ajax di Yii cukup mudah. Di View cukup tambahkan code seperti ini untuk linknya .
echo CHtml::ajaxLink( "Link Text", Yii::app()->createUrl('myController/ajaxRequest'), array(// ajaxOptions 'type' => 'POST', 'beforeSend' => "function( request ) { // Set up any pre-sending stuff like initializing progress indicators }", 'success' => "function( data ) { // handle return data alert( data ); }", 'data' => array('val1' => '1', 'val2' => '2') ), array(//htmlOptions 'href' => Yii::app()->createUrl('myController/ajaxRequest'), 'class' => $class ) );Pastikan user dapat mengakses controller yang dituju.
public function accessRules() { return array( array('allow', 'actions' => array('ajaxrequest'), 'users' => array('@'), ), // ... array('deny', // deny all users 'users' => array('*'), ), ); }Dan yang terkahir di action controllernya
public function actionAjaxRequest() { $val1 = $_POST['val1']; $val2 = $_POST['val2']; // // Perform processing // // // echo the AJAX response // echo "some sort of response"; Yii::app()->end(); }
0 comments:
Post a Comment