Chtml::ajax Doesn't Fire The Controller Action

Hi,

I am trying to use CHtml::ajax() function to send a request from the view to the controller.

The view display 4 input fields which it sends to the controller upon submit.

The controller makes an internal calculation and creates a request id ($reqid) which it send back to the view using render(). This part works as it should.

The view (Diag.php)


<?php

/* @var $this DiagFormController */

/* @var $model DiagForm */

/* @var $form CActiveForm */

?>


<div class="form">


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'diag-form-Diag-form',

	'enableAjaxValidation'=>true,

)); ?>


	<p class="note">Fields with <span class="required">*</span> are required.</p>


<?php echo $form->errorSummary($model); ?>

        

    <div class="row">

        <?php echo $form->label($model,'site_id'); ?>

        <?php echo $form->textField($model,'site_id') ?>

    </div>

    

    <div class="row">

        <?php echo $form->label($model,'link_id'); ?>

        <?php echo $form->textField($model,'link_id') ?>

    </div>

    

    <div class="row">

        <?php echo $form->label($model,'device_name'); ?>

        <?php echo $form->textField($model,'device_name') ?>

    </div>

    

    <div class="row">

        <?php echo $form->label($model,'device_level'); ?>

        <?php echo $form->textField($model,'device_level') ?>

    </div>

        

    <div class="row submit">

        <?php echo CHtml::button('Get Diagnostics', array('submit' => array('Diag/getDiagnostics'))); ?>

    </div>

        

    <div class="row">

        <?php echo $form->label($model,'result:'); ?>

        <?php echo $form->textArea($model,'result', array('maxlength' => 300, 'rows' => 10, 'cols' => 80)); ?>       

        

        <?php 

        if ($ajax_flag == 'true')

        {

             CHtml::ajax(array(

             'url'=>yii::app()->createUrl('Diag/CheckStatus'), 

             'type'=>'POST',

             'data'=>array('request'=>$reqid),

             'success'=>"function(){

                alert('ok');}"

             ));

       }

       ?>

    </div>


<?php $this->endWidget(); ?>

</div><!-- form -->



The view is trying to make ajax call to a different controller action (actionCheckStatus) however the function is never called.

The controller (DiagController.php)




class DiagController extends Controller

{

       

    

        public function actionIndex()

	{

       

	}

        

        public function actionGetDiagnostics()

        {

             // Internal logic which works fine

        }

        

        public function actionCheckStatus()

        {

            // Function never gets called !!!

        }



Notes:

I am using yii 1.1.13.

I am working on a localhost (Apache).

I tried replacing CHTML::ajax() with CHTML::ajaxLink() and it worked…

Again it seems that the ajax call doesn’t reach the controller (debugging verifies this).

What am I doing wrong?

Thanks for your help

I believe you actually need to ‘echo’ this out. So you’d change your code to:


        

        <?php 

        if ($ajax_flag == 'true')

        {

             echo CHtml::ajax(array(

             'url'=>yii::app()->createUrl('Diag/CheckStatus'), 

             'type'=>'POST',

             'data'=>array('request'=>$reqid),

             'success'=>"function(){

                alert('ok');}"

             ));

       }

       ?>







Thanks,

I already tried it with echo but it did not work.

Still nothing is called on the controller side.

I think your controller action is not called from ajax function.

used $this->createurl or

used Yii::app()->createAbsoluteUrl("controller/action").

did you check ajax request in console?

Hi Ravi,

In my post I used yii::app()->createUrl which did not work.

I also tried yii::app()->createAbsoluteUrl which did not work.

Hi mbala,

What do you mean check ajax request in console?

When I debug the application (xdebug) I see CHtml::ajax() is called, I even debugged into yii code and copied the created url to a different browser window and got the desired result (i.e. the created url called the correct controller action).

From my experience i added the possible problems?

  1. Check the case sensitive in url [if urlmanager configured]

  2. If Login required for action

If you can, post the controller code or try ajaxSubmitButton method

Hi,

  1. I checked the case sensitive issue. I posted before that almost the same url is working with ajaxLink function plus I copied the created url during debug to a different browser window and it activated the controller.

  2. What I posted is the controller code. I just omitted internal logic. there are only 3 functions nothing more at the moment.

Perhaps this is the problem maybe I need to override some function to make it work.

Hi skuchuk,

What about csrf token?

If you have enabled csrf validation, I think you have to include the csrf token manually in the ajax call.




echo CHtml::ajax(array(

    'url'=>yii::app()->createUrl('Diag/CheckStatus'), 

    'type'=>'POST',

    'data'=>array(

        'request'=>$reqid,

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

    ),

    'success'=>"function(){

        alert('ok');

    }"

));




Hi softark.

CSRF is not part of my config file (main.php) and is disabled by default.

I see.

Well, then I think you should do the debugging in the client side as mbala is suggesting.

Does your final HTML output have the script to fire ajax as you expect it to be? Or does the script work as expected?

You can do the client side debugging using the browser’s development tools. (In mbala’s reply, “console” means the javascript console of the browser. All latest browsers have nice tools for developers.)

?? wait a moment.

Does echoing CHtml::ajax() alone have some meaning? I mean, without an event triggering it.

What event do you want to fire the ajax?

I have a PHP parameter which I use to enable/disable the ajax call ($ajax_flag).

The controller is setting the flag to ‘true’ and renders the view.

while the view is rendered and the flag equals ‘true’, the ajax is being called (using xdebug I can see it gets called).

You must not confuse the client side javascript with the server side PHP program.

$ajax_flag in your view script only controls whether to output the javascript or not in the server side. But it’s another story whether the output script works fine or not in the client side.

Please trace the javascript code in the browser’s developer tool, not with xdebug in your IDE.

Thanks! That was it, beginners mistake.

Hi,

Also u can use this


Yii::app()->getController()->redirect(array('ticket/index?event_id='.$event_id.'&vid='.$data1['vid'].'&'));