title: Error: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
body: <p>I have the following code below, that I use to add products to the cart, but every time I add a product to the cart, the product is added to the cart, but i'm getting the error: unexpected end of data at line 1 column 1 of the JSON data is shown in firefox.
  Now in chrome, i'm getting the error: Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse () at Object.success</p>
  
  <p>I already tried to use console.log but php is not returning to me any errors. I tried several solutions but I could not solve them, so I came to ask for help here.</p>
  
  <p>jQuery:</p>
  
  <pre><code>$(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.
          });
  });
  
  })
  </code></pre>
  
  <p>PHP: Path /cart/add</p>
  
  <pre><code>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-&gt;selecionaproduto(array("prod_id" =&gt; $_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 . '&lt;li&gt;
                    &lt;span class="item"&gt;
                        &lt;span class="item-left"&gt;
                            &lt;img src="/web-files/img/course/' . $valor2['id'] . '/' . $valor2['top_nav_cart_thumbnail'] . '" alt=""&gt;
                            &lt;img src="/web-files/img/course/' . $valor2['id'] . '/' . $valor2['top_nav_cart_thumbnail'] . '" alt=""&gt;
                            &lt;span class="item-info"&gt;
                                &lt;span&gt;' . $valor2['name'] . '&lt;/span&gt;
                                &lt;span&gt;&lt;strong&gt;R$ ' . number_format($valor2['online_price'] . $valor2['classroom_price'], 2, ',', '.') . '&lt;/strong&gt;&lt;/span&gt;
                            &lt;/span&gt;
                        &lt;/span&gt;
                        &lt;span class="item-right"&gt;
                            &lt;button data-id="' . $valor2['id'] . '" class="btn btn-xs btn-danger pull-right delete-cart-item"&gt;x&lt;/button&gt;
                        &lt;/span&gt;
                    &lt;/span&gt;
                &lt;/li&gt;';
          }
  
          $frontend_cart = $frontend_cart . '&lt;li class="divider"&gt;&lt;/li&gt;
                                    &lt;li&gt;&lt;a class="text-center" href="/checkout"&gt;Cart&lt;/a&gt;&lt;/li&gt;
                                    &lt;li class="divider"&gt;&lt;/li&gt;
                                    &lt;li&gt;&lt;a class="text-center" href="/checkout"&gt;Checkout&lt;/a&gt;&lt;/li&gt;';
  
  
          $frontend_cart_result = array(
              "cart_li" =&gt; $frontend_cart,
              "cart_item_quantity" =&gt; count($_SESSION['cart'])
          );
  
          echo json_encode($frontend_cart_result);
      }
  }
  </code></pre>
  
  <p>here is the console.log </p>
  
  <pre><code>{"cart_li":"&lt;li&gt;\r\n                      &lt;span class=\"item\"&gt;\r\n                          &lt;span class=\"item-left\"&gt;\r\n                              &lt;img src=\"\/web-files\/img\/curso\/1\/psicofarmacologia-na-infancia-e-adolescencia-top-nav-cart.jpg\" alt=\"\"&gt;\r\n                              &lt;span class=\"item-info\"&gt;\r\n                                  &lt;span&gt;Curso de atualiza&amp;ccedil;&amp;atilde;o em psicofarmacologia na inf&amp;acirc;ncia e adolesc&amp;ecirc;ncia&lt;\/span&gt;\r\n                                  &lt;span&gt;&lt;strong&gt;R$ 999,00&lt;\/strong&gt;&lt;\/span&gt;\r\n                              &lt;\/span&gt;\r\n                          &lt;\/span&gt;\r\n                          &lt;span class=\"item-right\"&gt;\r\n                              &lt;button data-id=\"1\" class=\"btn btn-xs btn-danger pull-right delete-cart-item\"&gt;x&lt;\/button&gt;\r\n                          &lt;\/span&gt;\r\n                      &lt;\/span&gt;\r\n                  &lt;\/li&gt;&lt;li class=\"divider\"&gt;&lt;\/li&gt;\r\n                                      &lt;li&gt;&lt;a class=\"text-center\" href=\"\/checkout\"&gt;Cart&lt;\/a&gt;&lt;\/li&gt;\r\n                                      &lt;li class=\"divider\"&gt;&lt;\/li&gt;\r\n                                      &lt;li&gt;&lt;a class=\"text-center\" href=\"\/checkout\"&gt;Checkout&lt;\/a&gt;&lt;\/li&gt;","cart_item_quantity":1}
  </code></pre>
  
  <p>Tank you!</p>