title: shopify ajax clear cart
body: <p>I am not to familiar with AJAX/Javascript so I am having a hard time doing something simple.</p>
  <p>I am trying to clear the cart using <a href="http://docs.shopify.com/support/your-website/5-themes/can-i-use-ajax-api" rel="nofollow noreferrer">Shopify's AJAX API</a> before someone adds an item to the cart.</p>
  <p>My code currently looks like this:</p>
  <pre><code>&lt;script&gt;
    function clearcart(){
      var my_button=$('input.addToCart');
      my_button.on('click',function(){
        $.ajax({
          type: &quot;POST&quot;,
          url: '/cart/clear.js',
          data: '',
          success: success,
          dataType: 'json',
          success: function() {
            return Shopify.clear();
          },
          error: function(XMLHttpRequest, textStatus) {
            /* error code */
          }
        });
        return false;
      });
    }
  &lt;/script&gt;
  &lt;input type=&quot;submit&quot; name=&quot;add&quot; value=&quot;Try Now&quot; class=&quot;addToCart&quot; onclick=&quot;clearcart();&quot; &quot;javascript:window.location='/cart' return false&quot;/&gt;
  </code></pre>