【问题标题】:Have multiple ajax request running asynchronously有多个异步运行的ajax请求
【发布时间】:2011-12-09 01:12:30
【问题描述】:

我在一个 magento 网站上工作,有一个部分可以使用 ajax 将产品加载到购物车中。这很好用,但一次只允许处理一个请求。当一个人发布到数据库时,您不能选择另一个产品,否则它将终止第一个请求。我的问题是如何(或者是否有可能)同时运行多个请求而不取消另一个?处理 POST 的操作是:

public function addAjaxAction()
{   
    $response   = array();
    $error      = false;
    $cart       = $this->_getCart();
    $params     = $this->getRequest()->getParams();
    if (isset($params['simple_sku']) && $params['simple_sku'] != '') 
        $product    = Mage::getModel('catalog/product')->load(Mage::getSingleton('catalog/product')->getIdBySku($params['simple_sku']));
    else  
        $product    = $this->_initProductInStock();
    $related    = $this->getRequest()->getParam('related_product');

    if (!$product) {
        $error = 'Can not add item to shopping cart';
    }else{
        try {
            $cart->addProduct($product, $params);                               
            if (!empty($related)) {
                $cart->addProductsByIds(explode(',', $related));
            }   
            $cart->save();  
            $this->_getSession()->setCartWasUpdated(true);
            Mage::dispatchEvent('checkout_cart_add_product_complete', array('product'=>$product, 'request'=>$this->getRequest()));
            $response['status'] = 'success';
            $response['cart'] =  $this->getLayout()->createBlock('checkout/cart_minicart')
                ->setTemplate('page/html/modals/added_to_bag.phtml')
                ->setChild(
                    'breads'                    ,$this->getLayout()->createBlock('modcheckout/cart_breadcrumbs')->setTemplate('page/html/modals/mini_crumb.phtml')
                )
                ->toHtml();
                $response['cartStatus'] = $this->getLayout()->createBlock('core/template')->setTemplate('checkout/cart/status.phtml')->toHtml();
                $response['checkoutNow'] = $this->getLayout()->createBlock('core/template')->setTemplate('checkout/cart/checkoutnow.phtml')->toHtml();
        }
        catch (Exception $e) {
            $error = 'Can not add item to shopping cart';
        }
    }
    if($error){
        $response['status'] = 'error';
        $response['message'] = $error; /*= $this->getLayout()->createBlock('modcore/messages')
            ->addMessage(new Mage_Core_Model_Message_Error($error))
            ->getGroupedHtml();*/
    }
    echo Zend_Json::encode($response);
}

以及处理响应的js:

function addQuickviewAjax() {
    if(productForm.validate()){ 
        if(Product<?php echo $_product->getId() ?>.validate()) {                        
            var params = 'product='+Product<?php echo $_product->getId() ?>.productId+'&qty='+Product<?php echo $_product->getId() ?>.currentQty+'&super_attribute[76]='+Product<?php echo $_product->getId() ?>.currentColor+'&super_attribute[491]='+Product<?php echo $_product->getId() ?>.currentSize;
            new Ajax.Request('/modcheckout/cart/addAjax', {
            parameters: params,
                onFailure: reportError, 
                onSuccess: function(transport) {
                    var json = transport.responseText.evalJSON();
                    if(json.status == 'error'){
                        alert (json.message);       
                    } else if(json.status == 'success') {                       
                        alert ('success');
                    }
                }
            });
        } else {
            return false;
        }
    } 
    return false;
}

谁能告诉我这一切,并告诉我如何/如果我可以使用该操作进行多个 POST 吗?

【问题讨论】:

  • 感谢您的链接,但这只是建议使用 .live(),这是一种已弃用的方法,而且我没有为这个项目使用 jquery 的奢侈。我真的不明白这与什么有什么关系我在问...

标签: ajax magento asynchronous


【解决方案1】:

浏览器倾向于限制同时请求的数量 - 因此不要发送多个请求,而是将单个 XHR 发送到多个产品添加操作。如需更好的想法,请查看Mage_Checkout_CartController::addgroupAction()

【讨论】:

    猜你喜欢
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    • 2015-01-17
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 2012-05-26
    • 2018-04-10
    相关资源
    最近更新 更多