如何解决"ajax 403 error forbidden in CAKEPHP 2.x"

How to resolve "ajax 403 error forbidden in CAKEPHP 2.x"

我正在尝试使用 ajax 在 cakephp 中调用来访问数据,但收到 403 禁止错误,如下所示。

GET http://localhost/ec/bazar/Products/color_switcher/3 
403 (Forbidden)
jquery.js:6 
x.ajaxTransport.sendjquery.js:6 
x.extend.ajax53:251 
colorSwitcher53:327 
onclick

我正在使用此代码拨打电话

function colorSwitcher(id){

        var testid = id;
       $.ajax({
                type: 'GET',
                url: '<?php echo $this->webroot; ?>Products/color_switcher/' + testid,
                error: function () {
                    console.log("error in ajax call");
                },
                success: function (data) {
                    $("#img-portion").html(data);
                },
            });

    }

控制器

public function color_switcher($testid = ''){
      $this->layout= 'ajax';
}

color_switcher.ctp

<?php echo "helooooooo"; ?>  

经过一番努力,我找到了解决办法

如果您使用 Auth,如果 controller/action 不在您的 $this->Auth->allow() 列表中,您需要确保您已登录。

或者简单地让它允许

public 访问
public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('action name');
    }

在我的例子中 'action name' 将是 color_switcher

确保你也将 debug 设置为 0,这可能会给你带来一些问题。

@Dunhamzzz here 解释