【问题标题】:jQuery .post returns whole page in CakePHPjQuery .post 在 CakePHP 中返回整个页面
【发布时间】:2014-01-25 10:02:23
【问题描述】:

使用 jquery .post 将数据传递给 php 时遇到问题。

HTML

<div class="wrp">
    <h1>Product <? echo $pd['Product']['name'] ?></h1>

    <input id="qty" type="text" name="qty">
    <input id="add" type="button" value="Add to cart">

</div>

<script type="text/javascript">
$(document).ready(
function()
{
$('#add').click(function()
{
        $.post(
            '/shop/addtocart',
            {
                qty : ('#qty').val();
            },
            function(data){
                alert(data);
            }
        )
}
);
}
);
</script>

和 /shop/addtocart 功能

public function addtocart(){

    if($this->request->is('ajax')){

    $qty = $_POST['id'];
    echo $qty;

    }

}

当我点击它时,它会提醒我所需的值,但在该值之后它会回显整个 html 代码。

请帮忙!

【问题讨论】:

    标签: javascript php jquery ajax cakephp


    【解决方案1】:

    解决了!

    刚刚添加了一个出口;回声后

    public function addtocart(){
    
        if($this->request->is('ajax')){
    
        $qty = $_POST['id'];
        echo $qty;
        exit;
    
        }
    }
    

    【讨论】:

      【解决方案2】:

      尝试改变:

      qty : ('#qty').val();
      

      到:

      qty : $('#qty').val();
      

      【讨论】:

        【解决方案3】:
        public function addtocart(){
           $this->layout = 'ajax';
           if($this->request->is('ajax')){
                $qty = $_POST['id'];
                echo $qty;
            }
        }
        

        【讨论】:

        • 你也可以试试$this-&gt;autoLayout = false;
        猜你喜欢
        • 1970-01-01
        • 2011-02-04
        • 1970-01-01
        • 1970-01-01
        • 2014-06-19
        • 2013-12-04
        • 1970-01-01
        • 2010-09-25
        • 1970-01-01
        相关资源
        最近更新 更多