有多个异步运行的ajax请求

时间:2011-12-09 01:12:30

标签: ajax magento asynchronous

我正在使用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?

1 个答案:

答案 0 :(得分:0)

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