未捕获的SyntaxError:JSON.parse:第2行第1列的意外字符

问题描述:

更新1 :我重新编写了代码,然后将在线项目放在网上,但还是一样.

UPDATE 1: I rewritte the code and put online project but still the same.

此功能将产品添加到购物篮中.我遇到此错误,并且不知道如何解决:

This function adds a product to the basket. I have this error and I don't know how to fix it:

未捕获的SyntaxError:JSON.parse:JSON数据第二行第1列的意外字符

Uncaught SyntaxError: JSON.parse: unexpected character at line 2 column 1 of the JSON data

这是我的jQuery/AJAX代码:

This is my jQuery/AJAX code:

function add_to_cart(id, type) {
  alert(id);
  var qty = jQuery('#qty' + id).val();
  var id_product_checked = '';
  if (typeof id === 'undefined') {
    id_product_checked = 'no';
  }

  if (qty > 0 && id_product_checked != 'no') {
    alert(qty);
    jQuery.ajax({
      url: "manage_cart.php",
      type: "post",
      data: 'qty=' + qty + '&type=' + type + '&id=' + id,
      success: function(result) {
        var data = jQuery.parseJSON(result);
        alert(result);
        swal("Good job!", "You clicked the button!", "success");
        jQuery('#shop_added_msg_' + id).html('(Dodana -' + qty + ')');
        jQuery('#totalCartProduct').html(data.totalCartProduct);
        //console.log(result);
      }
    });
  } else {
    swal("Error", "error");
  }
}

manage_cart.php 这是负责将数据发送到mysql数据库的文件

manage_cart.php This is the file responsible for sending data to the mysql database

$qty=($_POST['qty']);
//$pdetail=($_POST['pdetail']);
$type=($_POST['type']);
$product_id=($_POST['id']);



    if($type=='add') {
        //isset($this->user_id)
        if($session2->is_logged_user_in()) {
            
        //$id=$_SESSION['user_id'];
        $user_id = $session2->user_id;
    
        $productcart = new Productcart($user_id,$qty,$product_id);  
            
        $productcart->manageUserCart($user_id,$qty,$product_id);
         
    
        } else {
            // gdy użytkownik jest wylogowany w sesji carta jest zapisywana
            $_SESSION['cart'][$product_id]['qty']=$qty;
            /*$totalProduct = count($_SESSION['cart']);*/
            echo 'add to cart';
        }
        
        $productcart2 = new Productcart();      
        $productcartfull = $productcart2->getUserFullCart();
        
        $totalProduct = count($productcartfull);
        $arr = array('totalCartProduct'=>$totalProduct);
        //header('Content-Type: application/json');
        echo json_encode($arr);
    }

解决方案正在为我工​​作.我删除了var data = jQuery.parseJSON(result);

Solution, is working for me. I removed var data = jQuery.parseJSON(result);

并添加了json with ajax.

和往常一样,我回答自己并找到解决方案:)他为此得到了-2.您正在寻找帮助,却没有得到

对于有善意的人.

 $(document).ready(function(){
     load_cart_data();
    });
        
        function load_cart_data() {
            $.ajax({
                url:"manage_cart3.php",
                method:"POST",
                dataType:"json",
                success:function(data)
                {

                   $('.total_price').text(data.totalPrice);
                   $('.badge').text(data.totalCartProduct);
                   console.log(data);
                }
        })
        }
    
    
        function add_to_cart(id,type) {
            //alert(id);
            var qty = jQuery('#qty' + id).val();
    
            if (qty > 0 ) {
            //alert(qty);
            jQuery.ajax({ 
                    url: 'manage_cart2.php',
                    type: 'post',
                    //data: 'qty=' + qty + '&type=' + type + '&id=' + id,
                    data:'id='+id+'&qty='+qty+'&type='+type,
                    success: function(result) {
                        load_cart_data(result);
                        swal("Dodane do koszyka");
                        jQuery('#shop_added_msg_' + id).html( '(Dodana -' + qty + ')');
                }
            });
    
            //load_cart_data();
    
            } else {
                swal("Błąd", "Proszę wybrać ilość.", "error");
            }
        }