2017-11-21 4 views
1

次のコードを使用してカートに商品を追加しますが、商品をカートに追加するたびに、製品がカートに追加されますが、エラーが発生します:JSONデータの1行目のカラム1の予期しないデータがfirefoxに表示されます。 クロムで、エラーが発生しました:Uncaught SyntaxError:Object.successのJSON.parse()でJSON入力が予期せず終了しましたエラー:JSON.parse:JSONデータの第1行第1列の予期しないデータ

私はすでにconsole.logを使用しようとしましたが、エラー。私はいくつかの解決策を試しましたが、解決できませんでしたので、ここで助けを求めるようになりました。

のjQuery:

$(document).ready(function() { 

//add a product in the cart 
$('#add-to-cart').click(function() { 

    $("#addtocartform").submit(function(e) { 
    var prod_id = $("#add-to-cart").data("id"); 
    var prod_mode = $("input[name=course_mode]:checked").val(); 


    $.ajax 
     ({ 
      url: '/cart/add', 
      type: 'POST', 
      data: jQuery.param({ prod_id: prod_id, prod_mode: prod_mode}), 
      dataType: 'text', 
      contentType: 'application/x-www-form-urlencoded; charset=UTF-8', 
      success: function (data) { 
       //console.log(data); 
       var frontend_cart_result = JSON.parse(data); 
       $('#dropdowncart').html(frontend_cart_result['cart_li']); 
       $('.badge').text(frontend_cart_result['cart_item_quantity']); 
       $(location).attr('href', '/checkout'); 

      }, 
      error: function() { 
       alert("error"); 
      } 

     }); 
     e.preventDefault(); // avoid to execute the actual submit of the form. 
     }); 
}); 

}) 

はPHP:パス/カート/ここ

public function add() { 

    //session_destroy(); 
    //$_SESSION['cart'][0]['id'] = 'teste'; 

    $cart_go = true; 
    if (!empty($_SESSION['cart'])) { 

     foreach ($_SESSION['cart'] as $valor) { 
      if ($valor['id'] == $_POST['prod_id']) { 
       $cart_go = false; 
      } 
     } 
    } 

    if ($cart_go) { 

     $db = new productModel(); 

     //check if product has already been added 
     if (!empty($_SESSION['cart'])) { 
      $next_key = max(array_keys($_SESSION['cart'])); 
      $next_key++; 
     } else { 
      $next_key = 0; 
     } 

     $_SESSION['cart'][$next_key] = $db->selecionaproduto(array("prod_id" => $_POST['prod_id'])); 
     //add all the products filds in session 
     //bought the online course 
     if ($_POST['prod_mode'] == 1) { 

      $_SESSION['cart'][$next_key]['classroom_price'] = ''; 
     } 
     //bought the classroom course 
     if ($_POST['prod_mode'] == 2) { 

      $_SESSION['cart'][$next_key]['online_price'] = ''; 
     } 


     $frontend_cart = ''; 


     foreach ($_SESSION['cart'] as $valor2) { 

      $frontend_cart = $frontend_cart . '<li> 
        <span class="item"> 
         <span class="item-left"> 
          <img src="/web-files/img/course/' . $valor2['id'] . '/' . $valor2['top_nav_cart_thumbnail'] . '" alt=""> 
          <img src="/web-files/img/course/' . $valor2['id'] . '/' . $valor2['top_nav_cart_thumbnail'] . '" alt=""> 
          <span class="item-info"> 
           <span>' . $valor2['name'] . '</span> 
           <span><strong>R$ ' . number_format($valor2['online_price'] . $valor2['classroom_price'], 2, ',', '.') . '</strong></span> 
          </span> 
         </span> 
         <span class="item-right"> 
          <button data-id="' . $valor2['id'] . '" class="btn btn-xs btn-danger pull-right delete-cart-item">x</button> 
         </span> 
        </span> 
       </li>'; 
     } 

     $frontend_cart = $frontend_cart . '<li class="divider"></li> 
            <li><a class="text-center" href="/checkout">Cart</a></li> 
            <li class="divider"></li> 
            <li><a class="text-center" href="/checkout">Checkout</a></li>'; 


     $frontend_cart_result = array(
      "cart_li" => $frontend_cart, 
      "cart_item_quantity" => count($_SESSION['cart']) 
     ); 

     echo json_encode($frontend_cart_result); 
    } 
} 

を追加するにconsole.log

{"cart_li":"<li>\r\n      <span class=\"item\">\r\n       <span class=\"item-left\">\r\n        <img src=\"\/web-files\/img\/curso\/1\/psicofarmacologia-na-infancia-e-adolescencia-top-nav-cart.jpg\" alt=\"\">\r\n        <span class=\"item-info\">\r\n         <span>Curso de atualiza&ccedil;&atilde;o em psicofarmacologia na inf&acirc;ncia e adolesc&ecirc;ncia<\/span>\r\n         <span><strong>R$ 999,00<\/strong><\/span>\r\n        <\/span>\r\n       <\/span>\r\n       <span class=\"item-right\">\r\n        <button data-id=\"1\" class=\"btn btn-xs btn-danger pull-right delete-cart-item\">x<\/button>\r\n       <\/span>\r\n      <\/span>\r\n     <\/li><li class=\"divider\"><\/li>\r\n          <li><a class=\"text-center\" href=\"\/checkout\">Cart<\/a><\/li>\r\n          <li class=\"divider\"><\/li>\r\n          <li><a class=\"text-center\" href=\"\/checkout\">Checkout<\/a><\/li>","cart_item_quantity":1} 

戦車あなたです!

+0

あなたが '' //console.log(データ) 'コメントアウト;' ''データの値は何ですか?他のコードは無関係です...値を参照する必要があります。おそらく不正形式です –

+1

[あなたのJSONを検証する](http://jsonlint.com/)とあなたの特定の問題を修正しないでください。 – mopsyd

+0

問題のconsole.log(date)を含めました – Rangel

答えて

1

応答のデータ型として、textと期待して、あなたのajax呼び出しに伝えています。次に、PHPスクリプトでHTMLを作成し、それをエンコーディングしてからajaxリクエストへの応答として出力します。

何かが間違っている可能性がありますか?

+0

パーフェクト、dataTypeをhtmlに変更しました。あなたはそんなにミクニクのタンク! – Rangel

+0

いいえ、データ型はjsonにする必要があります... – iep

0

あなたのjsonレスポンスに値としてHTMLを保存しないことをお勧めします。あなたのAJAXリクエストトライで

dataType: 'json', 
関連する問題