dongzhaoshi8497 2013-08-28 12:57
浏览 65
已采纳

403 Yii中的Ajax调用权限错误?

I am new to Yii. I have created a module which has below codes:

Controller's Code:

public function accessRules()
{
    return array(
        array('allow',  // allow all users to perform 'index' and 'view' actions
            'actions'=>array('index','view'),
            'users'=>array('*'),
        ),
        array('allow', // allow authenticated user to perform 'create' and 'update' actions
            'actions'=>array('create','update'),
            'users'=>array('@'),
        ),
        array('allow', // allow admin user to perform 'admin' and 'delete' actions
            'actions'=>array('admin','delete','ajaxdialog'),
            'users'=>array('*'),
        ),
        array('deny',  // deny all users
            'users'=>array('*'),
        ),
    );
}

AND here is action i want to use by ajax call:

public function actionAjaxdialog()
{
    $user_id =  $_POST['image_id'];     
    $is_active = $_POST['status'];
    $model = $this->loadModel($image_id);
    $model->is_active = $is_active;
    $model->update();   
}

view's code:

$(document).ready(function(){
$('.changeStatus').live('click',function(){
    var status = $(this).attr('status');
    if(status==1)
    {
        status=0;
    }
    else
    {
        status=1;
    }
    var id = $(this).attr('image_id');      
    var $link = $(this);

    $.ajax({

        type: "POST",
        url:    "<?php echo Yii::app()->createUrl('Gallery/Ajaxdialog'); ?>",
        data:  {image_id:id,status:status},
        success: function(msg)
        {   
            if(status==0)
            {
                $link.attr('status','0');
                $link.attr('title','In-active :: click to activate.');
                $link.children('img').attr('src','/images/wrong.png');
                AfterStatusChange('Image has been de-activated sucessfully.');
                $link.parent().parent().removeClass('selected');
            }
            else
            {
                $link.attr('status','1');
                $link.attr('title','Active :: click to de-activate.');
                $link.children('img').attr('src','/images/right.png');
                AfterStatusChange('Image has been activated sucessfully.');
                $link.parent().parent().removeClass('selected');
            }


        },
        error: function(xhr){
            //alert("failure"+xhr.readyState+this.url)
            alert("failure"+xhr.responseText);
        }
    });


});
});

I am working on localhost, i have update ".htaccess" file with following code:

deny from all
Allow from localhost

It always gives me the following error when i tries to run it:

"Access forbidden"

Please help me how can i resolve this issue.

  • 写回答

2条回答 默认 最新

  • doujiang5211 2013-10-19 03:22
    关注

    I managed to fix the forbidden issue with the following Ajax code:

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

    On the controller:

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

    You may try with this one.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c