XMLHttpRequest + Magento,Second Request重定向到主页

时间:2018-04-25 08:56:33

标签: php ajax magento controller xmlhttprequest

基本上,我正在向我的控制器发出请求:

Class Hrouter_Magelite_Category_ViewController extends Mage_Core_Controller_Front_Action
{
    private $_urlRewritesAction = false;

    function ajaxAction(){
        $filters = json_decode($_POST['filters'] , true);

        $_collection =  Mage::getModel('catalog/category')->load($_GET['id'])->getProductCollection();

        $_collection->setStoreId(Mage::app()->getStore()->getId());

        $_collection
        ->addAttributeToSelect('name')
        ->addAttributeToSelect('price')
        ->addAttributeToSelect('special_price')
        ->addAttributeToSelect('url_key')
        ->addAttributeToSelect('media_gallery')
        ->addAttributeToSelect('image_small')
        ->addAttributeToFilter('visibility' , 4)
        ->addAttributeToFilter('status' , 1)
        ->setCurPage($page)
        ->setPageSize($size);

        foreach($filters as $attr => $filter)
        {
            if($attr == 'price')
            {
                //custom handler
            }
            else{
                $_collection->addAttributeToFilter($attr , array('eq' => explode("," , $filter)));
            }
        }

        $items = array();

        foreach($_collection as $item)
        {
            $img = Mage::helper('catalog/image')->init( $item , 'small_image')->keepFrame(true)->resize(320 , 320);
            $items[] = array(
                'name' => $item->getName() , 
                'price' => $item->getPrice() , 
                'special_price' => $item->getSpecialPrice() , 
                'url_key' => $item->getUrlKey() , 
                'media_gallery' => strval($img) , 
                'image_small' => $item->getSmallImage()
            );
        }

        die(json_encode($items));
    }

我的indexAction函数工作正常,这就是我正在加载主页面,所以我知道Controller已正确配置。

我的Ajax代码是:

this.UpdateFilter = function(){
    var filters = document.querySelectorAll("ul.filters .filter li input");


    for(var i = 0;i < filters.length;i++)
    {
        var filter = filters[i];
        if(typeof this.filterPost[filter.name] === "undefined")
        {

            //create
            if(filter.type == 'checkbox'){
                if(filter.checked){
                    this.filterPost[filter.name] = (filter.checked ? filter.value : '');
                    filter.parentNode.className = 'active';
                }else{
                    filter.parentNode.removeAttribute("class");
                }
            }else if(filter.type == 'text')
            {
                if(filter.value == '') continue;
                this.filterPost[filter.name] = filter.value;


            }else{
                if(filter.value == '') continue;
                this.filterPost[filter.name] = filter.value;

            }
        }else{

            //create
            if(filter.type == 'checkbox'){
                if(filter.checked){
                    this.filterPost[filter.name] += "," + (filter.checked ? filter.value : '');
                    filter.parentNode.className = 'active';
                }else{
                    filter.parentNode.removeAttribute("class");
                }
            }else if(filter.type == 'text')
            {
                if(filter.value == '') continue;
                this.filterPost[filter.name] = "" + filter.value;


            }else{
                if(filter.value == '') continue;
                this.filterPost[filter.name] = "" + filter.value;

            }
        }
    }
    this.filterPost.price = {
        min: this.PriceMin , 
        max: this.priceMax
    };
    //end of filters

    console.log(this.filterPost);

    //send ajax request

    jQuery("body").mLoading('show');

    var url = "https://fresh.jejamescycles/magelite/category_view/ajax/?id=" + this.CategoryId;

    var x = new XMLHttpRequest();
    x.open("POST" , url , true);
    console.log("Requesting: " + "/magelite/category_view/ajax?id=" + this.CategoryId);
    x.onreadystatechange = function() {
        if(x.status == 200 && x.readyState == 4)
        {
            if(url != x.responseURL){
                jQuery("body").mLoading('hide');
                document.write("AJAX ERROR , Final Response URL: " + x.responseURL);
            }
            //success
            jQuery("body").mLoading('hide');

            try
            {
                var resp = JSON.parse(x.responseText);

                if(resp.length < 1)
                {
                    throw new Error("No Products Found");
                }

                var e = document.querySelector(".products ul");
                //container 
                e.innerHTML = '';

                for(var i = 0;i < resp.length;i++)
                {
                    var product = resp[i];

                    var html = '<li><a href="/' + product.url_key + '.html"><img src="' + product.media_gallery + '"/><p>' + product.name + '</p>';

                        if(product.special_price < product.price)
                        {
                            html += '<div class="highlight-deal">!</div><span class="price"><strike>Was ' + product.price + '</strike>&nbsp;&nbsp;<font style="font-size: 20px;font-weight:900;" color="orange"> Save ';

                            var diff = product.price - product.special_price;

                            var perc = diff / product.price * 100;

                            html += perc + '%</font><br><b style="font-size:30px;font-weight: 900;">' + product.special_price + '</b></span>';
                        }else{
                            html += '<span class="price"><b>' + product.price + '</b></span>';

                            html += '</a>';
                        }
                    html += '</li>';

                    e.innerHTML += html;
                }
            }catch(e)
            {
                document.querySelector(".products").innerHTML = '<p>No Products Found</p>';
            }
        }
    }
    x.setRequestHeader("Content-Type" , "application/x-www-form-urlencoded");
    x.send("filters=" + JSON.stringify(this.filterPost) + "&form_key=" + FORM_KEY);
};

我基本上是在创建一个AJAX Powered Catalog Layer。当我发出第一个请求时,它工作正常,并产生预期的结果。然而,第二次,请求重定向到主页,对于我的生活我无法找到原因,任何想法?

非常感谢!

- 编辑[添加了开发者控制台截图。 ] - 3个错误与问题无关。 DEV CONSOLE NETWORKING TAB..

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。我的收集调用略有错误。

foreach($filters as $attr => $filter)
            {
                if($attr == 'price')
                {
                    //custom handler
                }
                else{
                    $data[$attr] = explode("," , $filter);

                    $values = explode("," , $filter);

                    $_filter = array();

                    foreach($values as $value)
                    {
                        $_filter[] = array(
                            'attribute' => $attr , 
                            'eq' => $value
                        );
                    }

                    $_collection->addAttributeToFilter($_filter);
                }
            }