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

时间:2017-06-10 22:26:17

标签: php jquery ajax

我创建了一个之前正在运行的查询,但当我试图以json格式获取数据虽然它不起作用我不知道为什么它不显示数据作为回报这里是我的代码。当我检查控制台时,返回数据没有显示,即使它是处理我想是这样,但我无法纠正错误的问题。任何人都可以帮助我。

$(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);
            }
        });
    }));
});

他是我为处理数据而创建的php代码

$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 个答案:

答案 0 :(得分:1)

你的php文件中的问题..

第一:不要回复json_encode()

的任何内容

第二次:评论您的foreach循环可能是问题的原因

所以你的代码应该是这样的

$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);

附加信息:如果您没有input[type=file]且未将文件从js传递到php,则可以使用.serialize()代替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);
            }
        });
    }));
});