title: Impossible to add product to cart ERROR prestashop AJAX
body: <p>My website is working fine until this early week, whenever customers try to add a product, it will come out with this error;</p>
  
  <blockquote>
    <p>Impossible to add the product to the cart.</p>
    
    <p>textStatus: 'parsererror'</p>
    
    <p>errorThrown: 'SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 3 of the JSON data'</p>
    
    <p>responseText:</p>
    
    <p>45b</p>
    
    <p>{
    "products": [ .....} 0</p>
  </blockquote>
  
  <p>But if I put <a href="https://www.domain.com.au" rel="nofollow">https://www.domain.com.au</a> instead of www.domain.com.au, the AJAX won't throw any error.</p>
  
  <p>There are few speculations I have here, but I have no solutions;
  what I can think of that there is whitespace I need to delete, but I don't know where. And that the code that I put into my blockcart.php caused it;</p>
  
  <blockquote>
    <p>global $smarty;</p>
    
    <p>$smarty->assign('customerDefaultGroup' , Customer::getDefaultGroupId(Context::getContext()->customer->id));</p>
  </blockquote>
  
  <p>But I tried to comment those out, it still throws me error.
  SO for now, I disabled the AJAX but customers has started to complain that they are directed to shopping cart every time they add product.</p>
  
  <p>And I don't know if this is related, I have random chars on my header every time I access the website.</p>
  
  <p>Help</p>
  
  <p>Following my ajax-cart.js for add function;</p>
  
  <pre><code>// add a product in the cart via ajax
      add : function(idProduct, idCombination, addedFromProductPage, callerElement, quantity, whishlist){
          if (addedFromProductPage &amp;&amp; !checkCustomizations())
          {
              alert(fieldRequired);
              return ;
          }
          emptyCustomizations();
          //disabled the button when adding to not double add if user double click
          if (addedFromProductPage)
          {
              $('#add_to_cart input').attr('disabled', true).removeClass('exclusive').addClass('exclusive_disabled');
              $('.filled').removeClass('filled');
          }
          else
              $(callerElement).attr('disabled', true);
  
          if ($('#cart_block_list').hasClass('collapsed'))
              this.expand();
          //send the ajax request to the server
          $.ajax({
              type: 'POST',
              headers: { "cache-control": "no-cache" },
              url: baseUri + '?rand=' + new Date().getTime(),
              async: true,
              cache: false,
              dataType : "json",
              data: 'controller=cart&amp;add=1&amp;ajax=true&amp;qty=' + ((quantity &amp;&amp; quantity != null) ? quantity : '1') + '&amp;id_product=' + idProduct + '&amp;token=' + static_token + ( (parseInt(idCombination) &amp;&amp; idCombination != null) ? '&amp;ipa=' + parseInt(idCombination): ''),
  
              success: function(jsonData,textStatus,jqXHR)
              {
                  // add appliance to whishlist module
                  if (whishlist &amp;&amp; !jsonData.errors)
                      WishlistAddProductCart(whishlist[0], idProduct, idCombination, whishlist[1]);
  
                  // add the picture to the cart
                  var $element = $(callerElement).parent().parent().find('a.product_image img,a.product_img_link img');
                  if (!$element.length)
                      $element = $('#bigpic');
                  var $picture = $element.clone();
                  var pictureOffsetOriginal = $element.offset();
  
                  if ($picture.size())
                      $picture.css({'position': 'absolute', 'top': pictureOffsetOriginal.top, 'left': pictureOffsetOriginal.left});
  
                  var pictureOffset = $picture.offset();
                  if ($('#cart_block')[0] &amp;&amp; $('#cart_block').offset().top &amp;&amp; $('#cart_block').offset().left)
                      var cartBlockOffset = $('#cart_block').offset();
                  else
                      var cartBlockOffset = $('#shopping_cart').offset();
  
                  // Check if the block cart is activated for the animation
                  if (cartBlockOffset != undefined &amp;&amp; $picture.size())
                  {
                      $picture.appendTo('body');
                      $picture.css({ 'position': 'absolute', 'top': $picture.css('top'), 'left': $picture.css('left'), 'z-index': 4242 })
                      .animate({ 'width': $element.attr('width')*0.66, 'height': $element.attr('height')*0.66, 'opacity': 0.2, 'top': cartBlockOffset.top + 30, 'left': cartBlockOffset.left + 15 }, 1000)
                      .fadeOut(100, function() {
                          ajaxCart.updateCartInformation(jsonData, addedFromProductPage);
                      });
                  }
                  else
                      ajaxCart.updateCartInformation(jsonData, addedFromProductPage);
              },
              error: function(XMLHttpRequest, textStatus, errorThrown)
              {
                  document.write("Impossible to add the product to the cart.\n\ntextStatus: '" + textStatus + "'\nerrorThrown: '" + errorThrown + "'\nresponseText:\n" + XMLHttpRequest.responseText);
                  //reactive the button when adding has finished
                  if (addedFromProductPage)
                      $('#add_to_cart input').removeAttr('disabled').addClass('exclusive').removeClass('exclusive_disabled');
                  else
                      $(callerElement).removeAttr('disabled');
              }
          });
      },
  </code></pre>
  
  <p>My ajax-cart.js customization to check the minimum quantity</p>
  
  <pre><code>var href = $(this).attr('href').split('=');
              if(typeof href[4] === "undefined")
              { qty = href[3];} //array position changed to 3 after URL rewrite enabled.
              else{ qty = href[4];}
  </code></pre>
  
  <p>My blockcart-jsan.tpl code;</p>
  
  <pre><code>{ldelim}
  "products": [
  {if $products}
  {foreach from=$products item=product name='products'}
  {assign var='productId' value=$product.id_product}
  {assign var='productAttributeId' value=$product.id_product_attribute}
      {ldelim}
          "id":            {$product.id_product},
          "link":          "{$link-&gt;getProductLink($product.id_product, $product.link_rewrite, $product.category, null, null, $product.id_shop, $product.id_product_attribute)|addslashes|replace:'\\\'':'\''}",
          "quantity":      {$product.cart_quantity},
          "priceByLine":   "{if $priceDisplay == $smarty.const.PS_TAX_EXC}{displayWtPrice|html_entity_decode:2:'UTF-8' p=$product.total}{else}{displayWtPrice|html_entity_decode:2:'UTF-8' p=$product.total_wt}{/if}",
          "name":          "{$product.name|html_entity_decode:2:'UTF-8'|escape:'htmlall'|truncate:15:'...':true}",
          "price":         "{if $priceDisplay == $smarty.const.PS_TAX_EXC}{displayWtPrice|html_entity_decode:2:'UTF-8' p=$product.total}{else}{displayWtPrice|html_entity_decode:2:'UTF-8' p=$product.total_wt}{/if}",
          "price_float":   "{$product.total}",
          "idCombination": {if isset($product.attributes_small)}{$productAttributeId}{else}0{/if},
          "idAddressDelivery": {if isset($product.id_address_delivery)}{$product.id_address_delivery}{else}0{/if},
          "is_gift" : {if isset($product.is_gift) &amp;&amp; $product.is_gift}1{else}0{/if},
  {if isset($product.attributes_small)}
          "hasAttributes": true,
          "attributes":    "{$product.attributes_small|addslashes|replace:'\\\'':'\''}",
  {else}
          "hasAttributes": false,
  {/if}
          "hasCustomizedDatas": {if isset($customizedDatas.$productId.$productAttributeId)}true{else}false{/if},
          "customizedDatas":[
          {if isset($customizedDatas.$productId.$productAttributeId[$product.id_address_delivery])}
          {foreach from=$customizedDatas.$productId.$productAttributeId[$product.id_address_delivery] key='id_customization' item='customization' name='customizedDatas'}{ldelim}
  {* This empty line was made in purpose (product addition debug), please leave it here *}
              "customizationId":  {$id_customization},
              "quantity":         "{$customization.quantity}",
              "datas": [
                  {foreach from=$customization.datas key='type' item='datas' name='customization'}
                  {ldelim}
                      "type": "{$type}",
                      "datas":
                      [
                      {foreach from=$datas key='index' item='data' name='datas'}
                          {ldelim}
                          "index":            {$index},
                          "value":            "{$data.value|addslashes|replace: '\\\'':'\''}",
                          "truncatedValue":   "{$data.value|truncate:28:'...'|addslashes|replace: '\\\'':'\''}"
                          {rdelim}{if !$smarty.foreach.datas.last},{/if}
                      {/foreach}]
                  {rdelim}{if !$smarty.foreach.customization.last},{/if}
                  {/foreach}
              ]
          {rdelim}{if !$smarty.foreach.customizedDatas.last},{/if}
          {/foreach}
          {/if}
          ]
      {rdelim}{if !$smarty.foreach.products.last},{/if}
  {/foreach}{/if}
  ],
  "discounts": [
  {if $discounts}{foreach from=$discounts item=discount name='discounts'}
      {ldelim}
          "id":              "{$discount.id_discount}",
          "name":            "{$discount.name|cat:' : '|cat:$discount.description|truncate:18:'...'|addslashes|replace:'\\\'':'\''}",
          "description":     "{$discount.description|addslashes|replace:'\\\'':'\''}",
          "nameDescription": "{$discount.name|cat:' : '|cat:$discount.description|truncate:18:'...'|addslashes|replace:'\\\'':'\''}",
          "code":            "{$discount.code}",
          "link":            "{$link-&gt;getPageLink("$order_process", true, NULL, "deleteDiscount={$discount.id_discount}")}",
          "price":           "{if $priceDisplay == 1}{convertPrice|html_entity_decode:2:'UTF-8' price=$discount.value_tax_exc}{else}{convertPrice|html_entity_decode:2:'UTF-8' price=$discount.value_real}{/if}",
          "price_float":     "{if $priceDisplay == 1}{$discount.value_tax_exc}{else}{$discount.value_real}{/if}"
      {rdelim}
      {if !$smarty.foreach.discounts.last},{/if}
  {/foreach}{/if}
  ],
  "shippingCost": "{$shipping_cost|html_entity_decode:2:'UTF-8'}",
  "shippingCostFloat": "{$shipping_cost_float|html_entity_decode:2:'UTF-8'}",
  {if isset($tax_cost)}
  "taxCost": "{$tax_cost|html_entity_decode:2:'UTF-8'}",
  {/if}
  "wrappingCost": "{$wrapping_cost|html_entity_decode:2:'UTF-8'}",
  "nbTotalProducts": "{$nb_total_products}",
  "total": "{$total|html_entity_decode:2:'UTF-8'}",
  "productTotal": "{$product_total|html_entity_decode:2:'UTF-8'}",
  {if isset($errors) &amp;&amp; $errors}
  "hasError" : true,
  "errors" : [
  {foreach from=$errors key=k item=error name='errors'}
      "{$error|addslashes|html_entity_decode:2:'UTF-8'}"
      {if !$smarty.foreach.errors.last},{/if}
  {/foreach}
  ]
  {else}
  "hasError" : false
  {/if}
  {rdelim}
  </code></pre>