【问题标题】:How to resolve "ajax 403 error forbidden in CAKEPHP 2.x"如何解决“CAKEPHP 2.x 中禁止出现 ajax 403 错误”
【发布时间】:2015-03-03 19:58:16
【问题描述】:

我正在尝试使用 cakephp 中的 ajax 调用访问数据,但出现 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"; ?>  

【问题讨论】:

    标签: jquery ajax cakephp


    【解决方案1】:

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

    如果您使用 Auth,如果控制器/操作不在您的 $this-&gt;Auth-&gt;allow() 列表中,则需要确保您已登录。

    或者干脆让它允许公众访问

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

    在我的情况下,“动作名称”将是 color_switcher

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

    由@Dunhamzzz 解释here

    【讨论】:

      猜你喜欢
      • 2018-02-21
      • 2016-10-15
      • 2016-06-19
      • 2019-12-27
      • 1970-01-01
      • 2020-05-01
      • 2017-11-11
      • 1970-01-01
      • 2023-03-05
      相关资源
      最近更新 更多