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 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算