500尝试使用ajax请求cakePHP 2中的500内部服务器错误

时间:2015-11-26 05:26:06

标签: ajax cakephp cakephp-2.0

如果尝试从购物车模型中的另一个型号(产品)获取数据,我会收到500内部服务器错误。使用ajax。但如果我只是评论$this->Product->findBy(id)工作正常。

$('form#add_form').on('submit', function(e){

        var thisForm = $(this);
        $.ajax({
            url: thisForm.attr('action'),
            type: 'POST',
            //dataType: 'json',
            data: thisForm.serialize(),
            success: function(count) {
                var total_items = $('p#total-items');
                total_items.html('Total items: ' + count);
                console.log(count);
            } 
        });
        e.preventDefault();
    });

这是我的CartsController

class CartsController extends AppController {
    public $uses = array('Product', 'Cart');

    public function add() {
        $this->autoRender = false;
        $itemsInCart = $this->Session->read();

        if ($this->request->is('POST')) {
            $item = $this->Product->findBy($this->request->data['Cart']['product_id']);
        }
        echo $this->request->data['Cart']['product_id'];
    }
}

1 个答案:

答案 0 :(得分:1)

没有findBy方法

您必须更改您的控制器代码:

class CartsController extends AppController {
    public $uses = array('Product', 'Cart');

    public function add() {
        $this->autoRender = false;
        $itemsInCart = $this->Session->read();

        if ($this->request->is('POST')) {
            $productID = $this->request->data['Cart']['product_id'];
            $item = $this->Product->findById($productID);
        }
        echo json_encode($this->request->data['Cart']['product_id']);
    }
}