0
votes

I have seen many other questions related to this one, I have tried their solutions but it still does not work (same error), reason why I am opening this thread.

Elements: 1 View --> Originates the AJAX request. 1 Controller --> Should handle the AJAX request and return an array.

View:

  $(document).ready(function(){
      $("button").click(function(){
              $("#test").load("<? echo >Yii::app()-createUrl('test/ajax');?>");
              });             

  });

So when I clicked a button with an "id=test", it should go to the TestController and from the actionAjax return a simple array.

The problem I am having is that whenever I click the #test button a 403 Forbidden log is seen in the console (Google Chrome console, for instance).

In the TestController I have the following access rules:

public function accessRules() {
return array(

 array('allow',

       'actions'=array('ajax'),

       'users'=array('@'),

 ),
 );    }

And of course I do have an action called actionAjax.

Am I missing something here? Is there any rule that I should take into account?

Am I free to make any Ajax call (load, post..) in Yii or there is a convention / function that I should follow?

Thanks for looking into this.

3
Might be a typo but you are missing a > character from your array syntax. Should be 'users' => array('@'),Kevin
Thanks for checking on this one. Actually, after adding the code to the question the ">" characters were gone, but I do have them correct. I get: Failed to load resource: the server responded with a status of 403 (Forbidden)Portu

3 Answers

0
votes

You're missing characters in your array syntax.

public function accessRules() {
return array(
array('allow',
   'actions'=>array('ajax'),
   'users'=>array('@'),
     ),
);
}
0
votes

It seems to work with this one:

$.ajax({

        url:"<?php echo Yii::app()->createUrl('test/ajax');?>",

        data:{},

        type:"POST",

        dataType:"html",

        success:function(response){

             $('#test').html(response);

        },

        error:function(){

             alert("Failed request data from AJAX request");

        }

    });

Thanks for your help!

0
votes

Forbidden means that you have to add that rule in accessRules of your controller:

class TestController extends Controller
{
    public function accessRules()
    {
        return array(
            array('allow',
                'actions' => array('ajax'),
                'users' => array('@'),
            ),
            array('deny',
                'users' => array('*'),
            ),
        );
    }
}

And take a look at your code because is full of typo:

$(document).ready(function(){
    $("button").click(function(){
        $("#test").load("<? echo Yii::app()->createUrl('test/ajax'); ?>");
    });             
});