douyou1960
2017-06-10 22:26
浏览 43
已采纳

无法通过ajax调用显示返回数据

I have created a query it was working before but when i tried to get data in json format though it is not working i do not know why it is not showing data in return here is my code. when I checked the console the return data is not showing though not even it is processing I think so but I am unable to rectify the problem the bug. can any one please help me out.

$(document).ready(function(e) {
    $("#addcart").on('submit', (function(e) {
        var oldval   = $('#crtcount').text();
        var form     = this;
        var formData = new FormData(this);
         $.ajax({
            url         : "includes/get_data.php",
            type        : "POST",
            data        : formData,
            contentType : false,
            cache       : false,
            processData : false,
            dataType    : "JSON",
            success     : function(data) {
                var data = $.parseJSON(data);
                $('#crtcount').html(parseInt(oldval)+1);
                $('#crtcount1').html(parseInt(oldval)+1);
                $('#cart_dt').html(data.cart);
                console.log(data.message);
            }
        });
    }));
});

his is my php code which I have created for processing the data

$qty = $_POST['qty'];
$pid = $_POST['pid'];
echo $cart->add_to_cart($pid);

$data['message']  = "<div class='message'>Your product is added to cart</div>";
$data['cart']     = '';
foreach($_SESSION['cart']['pid'] as $content) { 
    $prod_id          = $content['prod_id'];
    $products         = bgMysqlSelect("SELECT * FROM bg_products WHERE pid = '$prod_id'");
    $data['cart']    .= '<div class="items-sm"><div class="img-sm"><img src="../includes/uploads'.$products['pimage'].'" /></div>';
    $data['cart']    .= '<div class="cart-inf-sm"><h4>'.$products['ptitle'].'</h4>';
    $data['cart']    .= '<p><span>price :</span>$'.$products['pmsrp'].'</p>';
    $data['cart']    .= '<p><span>Qty :</span>'.$content['qty'].'</p>';
    $data['cart']    .= '<a href=""><i class="fa fa-times"></i></a></div></div>';
}

echo json_encode($data);
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongyi2993 2017-06-12 17:21
    已采纳

    The problem in your php file ..

    1st: Don't echo any thing with json_encode()

    2nd: Comment your foreach loop It may be the cause of the problem

    So your code should be something like this

    $qty = $_POST['qty'];
    $pid = $_POST['pid'];
    //echo $cart->add_to_cart($pid);
    
    $data['message']  = "<div class='message'>Your product is added to cart</div>";
    $data['cart']     = '';
    /*foreach($_SESSION['cart']['pid'] as $content) { 
        $prod_id          = $content['prod_id'];
        $products         = bgMysqlSelect("SELECT * FROM bg_products WHERE pid = '$prod_id'");
        $data['cart']    .= '<div class="items-sm"><div class="img-sm"><img src="../includes/uploads'.$products['pimage'].'" /></div>';
        $data['cart']    .= '<div class="cart-inf-sm"><h4>'.$products['ptitle'].'</h4>';
        $data['cart']    .= '<p><span>price :</span>$'.$products['pmsrp'].'</p>';
        $data['cart']    .= '<p><span>Qty :</span>'.$content['qty'].'</p>';
        $data['cart']    .= '<a href=""><i class="fa fa-times"></i></a></div></div>';
    }*/
    
    echo json_encode($data);
    

    ِAdditional Information: while you have no input[type=file] and not passing files from js to php you can use .serialize() instead of new formData()

    $(document).ready(function(e) {
        $("#addcart").on('submit', (function(e) {
            e.preventDefault();
            var oldval   = $('#crtcount').text();
            var form     = $(this);
            var formData = $(this).serialize();
             $.ajax({
                url         : "includes/get_data.php",
                type        : "POST",
                data        : formData,
                //contentType : false,
                //cache       : false,
                //processData : false,
                dataType    : "JSON",
                success     : function(data) {
                    //var data = $.parseJSON(data); // I think there is no need for this line
                    $('#crtcount').html(parseInt(oldval)+1);
                    $('#crtcount1').html(parseInt(oldval)+1);
                    $('#cart_dt').html(data.cart);
                    console.log(data.message);
                }
            });
        }));
    });
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题