0

我正在尝试从控制器传递数据以在视图中使用。正在发生的事情是我正在为用户选择的产品(尺寸和颜色)获取超级属性,所有这些都被序列化并传递给我获得产品对象的控制器。然后,我可以从这个对象获取可配置的 sku,然后我需要将其回显到设计 view.phtml 文件中。

控制器中的功能如下所示:

if($product = $this->_initProduct())
        {
           $config = $product->getSku();
          // if I echo $config here using firephp I can see that this is correct value
           Mage::register('config_product', $config);
          // I am hoping to set it as a global variable here that 
          //  I can then retrieve on the success of the ajax function
        }

在 phtml 中,我有类似的东西可以将表单值发送到该控制器:

 var data = $("#product_addtocart_form").serialize();
            $.ajax({
                url: "/locator/item/index",
                data: data
            }).done(function(){
                <?php $currentProduct = Mage::registry('config_product'); 
                 // Why does this return blank??
                 ?>
                 <?php Mage::helper('firephp')->send( $currentProduct ); ?>                    
            });
4

1 回答 1

1

PHP 和 JavaScript 不能这样一起使用。PHP 用于 Web 服务器,JavaScript 安装在客户端浏览器(IE、Chrome 等)中

在您的控制器中,您需要回显一个 JSON 编码的字符串,并在您的 javascript 中将 dataType 添加到 .ajax 函数:

        $.ajax({
            url: "/locator/item/index",
            data: data,
            dataType: 'json'
        },function(returnedData) {
            console.log(returnedData);                   
        });

并在浏览器中打开 js 控制台以检查返回的数据。

于 2013-02-22T23:45:18.203 回答