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

无法通过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);
                }
            });
        }));
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看