user1496306
user1496306

Reputation:

Yii Jquery AJAX Call not firing

I have been researching and trying to learn the proper method to accomplish this. I have done the following but, I still have no change on my form. Is there anyway to see if the ajax call is even being made? Could anyone correct this code to figure out why I am not getting any change on the field in my form.

In my model I have the following function

     public function standardRate($country) 
    { 

    $country_std_rate = Yii::app()->db->createCommand()       
       ->select('AVG(rate_syr_mh)')       
       ->from('packaging_metrics')       
       ->where(array('like', 'country', '%'.$country.'%'))    
       ->queryRow();    

            echo CJSON::encode($country_std_rate);  

    }    

In my controller I have added this function as well as added 'countryRate' to my access control for all

public function countryRate() 
    { 
            $input_country = $_POST['country']; 

            $model = PackagingMetric::model()->standardRate($input_country); 
            echo CJSON::encode($model);  

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

            $this->render('countryRate',array( 
                    'model'=>$model, 
            )); 

    }

This is my Jquery call in my form

  $('#PackagingMetric_std_rate').live('click',function() 
    {  

 var country =  $('#country').val();  

       $.ajax({  
               type: 'POST',  
               url: 'PackagingMetric/countryRate',  
               data: {country:country},  

                           success: function(data)  
                               {                                                      

                $('#PackagingMetric_std_rate').html(data); 

               } 

                               }); 

             });

However, nothing on my form changes with a value, I am using Yii-Debug-toolbar and not seeing any POST calls or any sql queries upon clicking the appropriate field on my form.

Upvotes: 2

Views: 4125

Answers (3)

Onkar Janwa
Onkar Janwa

Reputation: 3950

I consider to use absolute url while firing an ajax call. Try to make your url in this way, it will solve your problem.

url: '<?php echo Yii::app()->createAbsoluteUrl('PackagingMetric/countryRate');           ?>',   

and your controller action name should be "actionCountryRate" because this function is hit from url, not internally called from controller, also set its accessRule if you are using access control otherwise the function will not execution.

Upvotes: 0

bool.dev
bool.dev

Reputation: 17478

Your url parameter for the $.ajax(); is wrong could be wrong.

That is if you are not hiding the entry script (index.php), you will have to write:

url: 'index.php/controllername/actionname'

instead of:

url: 'controllername/actionname'

It would be better to use createUrl():

url: '<?php echo $this->createUrl('PackagingMetric/countryRate'); ?>'

(Ofcourse the echo above and how you include php code in js will depend on how you are including the js in the first place)

And name your countryRate as actionCountryRate().

Upvotes: 1

ernie
ernie

Reputation: 6356

Rename the function in the controller to actionCountryRate()

When a post request is made to a controller, it looks for an action using the action prefix.

Upvotes: 2

Related Questions