0

我是 Yii 的新手。我创建了一个具有以下代码的模块:

控制器代码:

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('*'),
        ),
    );
}

这是我想通过 ajax 调用使用的操作:

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

视图的代码:

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


});
});

我正在localhost上工作,我使用以下代码更新了“ .htaccess ”文件:

deny from all
Allow from localhost

当我尝试运行它时,它总是给我以下错误:

“禁止访问”

请帮助我如何解决这个问题。

4

2 回答 2

0

我设法使用以下 Ajax 代码解决了禁止的问题:

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

在控制器上:

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

你可以试试这个。

于 2013-10-19T03:22:31.597 回答
0

在 ajax 数据中添加 csrfToken。

$.ajax({

        type: "POST",
        url:    "<?php echo Yii::app()->createUrl('Gallery/Ajaxdialog'); ?>",
        data:  {<?= Yii::$app->request->csrfParam; ?> : '<?= Yii::$app->request->csrfToken; ?>',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);
        }
    });
于 2017-03-29T06:28:03.213 回答