Yii Framework Forum: Chtml::ajax Doesn't Fire The Controller Action - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Chtml::ajax Doesn't Fire The Controller Action Ajax request not working Rate Topic: -----

#1 User is offline   skuchuk 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 11-June 13

Posted 01 July 2013 - 11:13 AM

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
0

#2 User is offline   enotirab 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 45
  • Joined: 14-January 11

Posted 01 July 2013 - 02:09 PM

View Postskuchuk, on 01 July 2013 - 11:13 AM, said:


        
        <?php 
        if ($ajax_flag == 'true')
        {
             CHtml::ajax(array(
             'url'=>yii::app()->createUrl('Diag/CheckStatus'), 
             'type'=>'POST',
             'data'=>array('request'=>$reqid),
             'success'=>"function(){
                alert('ok');}"
             ));
       }
       ?>







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');}"
             ));
       }
       ?>




0

#3 User is offline   skuchuk 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 11-June 13

Posted 02 July 2013 - 01:19 AM

View Postenotirab, on 01 July 2013 - 02:09 PM, said:

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.
0

#4 User is offline   Ravi P Thanki 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 61
  • Joined: 18-March 13
  • Location:Ahmedabad

Posted 02 July 2013 - 02:56 AM

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

used $this->createurl or

used Yii::app()->createAbsoluteUrl("controller/action").
Ravi Thanki
Yii,Cakephp and Magento Developer,
blog:http://ravithanki.wordpress.com
0

#5 User is offline   mbala 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 155
  • Joined: 21-July 12
  • Location:Chennai, Tamilnadu, India

Posted 02 July 2013 - 04:40 AM

did you check ajax request in console?
0

#6 User is offline   skuchuk 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 11-June 13

Posted 02 July 2013 - 06:36 AM

View Postmbala, on 02 July 2013 - 04:40 AM, said:

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).
0

#7 User is offline   mbala 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 155
  • Joined: 21-July 12
  • Location:Chennai, Tamilnadu, India

Posted 02 July 2013 - 07:11 AM

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
0

#8 User is offline   skuchuk 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 11-June 13

Posted 02 July 2013 - 07:25 AM

View Postmbala, on 02 July 2013 - 07:11 AM, said:

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.
0

#9 User is online   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,041
  • Joined: 16-February 11
  • Location:Japan

Posted 02 July 2013 - 09:05 AM

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');
    }"
));


0

#10 User is offline   skuchuk 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 11-June 13

Posted 02 July 2013 - 09:21 AM

View Postsoftark, on 02 July 2013 - 09:05 AM, said:

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.
0

#11 User is online   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,041
  • Joined: 16-February 11
  • Location:Japan

Posted 02 July 2013 - 10:15 AM

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.)
0

#12 User is online   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,041
  • Joined: 16-February 11
  • Location:Japan

Posted 02 July 2013 - 10:23 AM

?? 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?
0

#13 User is offline   skuchuk 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 11-June 13

Posted 02 July 2013 - 10:50 AM

View Postsoftark, on 02 July 2013 - 10:23 AM, said:

?? 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).
0

#14 User is online   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,041
  • Joined: 16-February 11
  • Location:Japan

Posted 02 July 2013 - 11:19 AM

View Postskuchuk, on 02 July 2013 - 10:50 AM, said:

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.
0

#15 User is offline   skuchuk 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 11-June 13

Posted 03 July 2013 - 07:07 AM

View Postsoftark, on 02 July 2013 - 11:19 AM, said:

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.
0

#16 User is offline   Ankit Modi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 866
  • Joined: 19-February 13
  • Location:India

Posted 03 July 2013 - 07:10 AM

Hi,

Also u can use this

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

Thanks,
Ankit Modi
Skype : amodi06
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users