0

我想使用拖放将产品添加到购物车。在 drop 函数中,我调用 Ajax 函数将产品添加到购物车:

url变量包含基于产品的结帐购物车 URL。

jQuery(function() {
jQuery(".category-products" ).accordion();
jQuery(".product-name" ).draggable({
  appendTo: "body",
  helper: "clone"
});
jQuery(".block-content ol" ).droppable({
  activeClass: "ui-state-default",
  hoverClass: "ui-state-hover",
  accept: ":not(.ui-sortable-helper)",
  drop: function( event, ui ) {
    jQuery( this ).find( ".placeholder" ).remove();
    var url =  ui.draggable.attr('id');
    jQuery.get(url, function(data) {
          // data is the response from the server..
          if(data.status == 'ERROR'){
    alert(data.message);
    }
          console.log(data); 
      });
         url += 'isAjax/1';
    url = url.replace("checkout/cart","draggableproduct/index");
    try {
        jQuery.ajax( {
            url : url,
            dataType : 'json',
            success : function(data) { 
                if(jQuery('.block-cart')){
            jQuery('.block-cart').replaceWith(data.sidebar);
        }
        if(jQuery('.header .links')){
            jQuery('.header .links').replaceWith(data.toplink);
        }
            }
        });
    } catch (e) {
    alert(e);
    }
        jQuery( "<li></li>" ).text( ui.draggable.text() ).appendTo( this );   
  }
}).sortable({
  items: "li:not(.placeholder)",
  sort: function() {
    // gets added unintentionally by droppable interacting with sortable
    // using connectWithSortable fixes this, but doesn't allow you to customize active/hoverClass options
    jQuery( this ).removeClass( "ui-state-default" );
  }
});
});

并且在 indexcontroller 代码上包含:-

$this->loadLayout();
                $toplink = $this->getLayout()->getBlock('top.links')->toHtml();
                $sidebar_block = $this->getLayout()->getBlock('cart_sidebar');
                Mage::register('referrer_url', $this->_getRefererUrl());
                $sidebar = $sidebar_block->toHtml();
                $response['toplink'] = $toplink;
                $response['sidebar'] = $sidebar;
                                    $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
        return ;

该产品已添加到购物车,但 Ajax 调用未完成,因为我没有得到任何响应,并且在丢弃 1 个产品后,我无法在购物车中放入更多产品。请帮我

4

2 回答 2

0

您可以使用

 jquery.post(url,callback).
于 2013-02-13T06:44:59.440 回答
0

使用成功回调.....

成功回调函数将传递返回的数据,该数据将是 XML 根元素、文本字符串、JavaScript 文件或 JSON 对象,具体取决于响应的 MIME 类型。它还传递了响应的文本状态。

jQuery.get(url, function(data) {
   // data is the response from the server..
   console.log(data);
});
于 2013-02-13T06:47:29.953 回答