【问题标题】:Returning values in MagentoMagento 中的返回值
【发布时间】:2014-05-21 07:06:11
【问题描述】:

以下是控制器中返回产品值的函数

public function productsAction()
     {

            $sku=$this->getRequest()->getparam('subvalue');
            $_product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);

            $upsell_product_collection = $_product->getUpSellProductCollection();
            $upsel=$upsell_product_collection->addAttributeToSelect('product_type')
            ->addAttributeToFilter('product_type', 133);

            $products1 = $_product->getData();
            return $products1;
     }

下面是前端取值的ajax请求

$.ajax
  ({
        type: "POST",
        url: "<?php echo $this->getUrl('finder/index/products');?>",
      data:"subvalue="+subval, 
      success: function(response) 
      {     
             alert(response);
            $("#responseproducts").html(response);

       };
  });

我无法在前端获取值..请告诉我在前端获取值的方法

【问题讨论】:

标签: magento magento-1.8


【解决方案1】:

在 php 中解码为 JSON:

return json_encode($products1);

并在 javascript 中解码:

JSON.parse(response);

这应该可以。之后您可以使用以下命令对其进行测试:

console.log(response);

【讨论】:

    【解决方案2】:

    如果您的 ajax 请求发送成功,则将此代码添加到您的控制器操作中。

    $response['prodcutdata'] = $products1;
    $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
    return;
    

    在 ajax 请求成功之后访问它,如下所示:

    alert(response.prodcutdata);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-18
      • 2013-07-22
      • 2018-06-15
      • 2016-02-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多