【问题标题】:Magento Ajax and Mage::getModelMagento Ajax 和 Mage::getModel
【发布时间】:2015-05-08 17:11:22
【问题描述】:

我正在使用 Magento 1.9,在页面 .phtml 中我有一个 AJAX 请求:

$('#dive').change(function() {
    if($(this).val() > 0) {
        $j.ajax({
            url: 'dominio.com/myfile.php',
            type: 'POST',
            data: { id: $(this).val() },
            success: function(data) {
                $('.classe').html(data);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert('error');
            }    
        });
    }
});

myfile.php 的内容是:

<?php 
     $id = $_POST['id'];
     echo $id;
?>

工作效果很好,$ ID在.classe @时,选择表单的值> 0,但我希望在myfile.php中有父类别的子类别,具有id = $ id

我尝试添加此代码:

$children = Mage::getModel('catalog/category')->getCategories($id);
foreach ($children as $subcategory) {
    echo $subcategory->getName();
}

该代码在 .phtml 中有效,但如果我将其添加到 myfile.php 中,我将一无所获。

有什么想法吗?

【问题讨论】:

    标签: php jquery ajax magento magento-1.9


    【解决方案1】:

    您可能会错过应用程序初始化。 如果这是你的整个 php 代码:

    <?php 
         $children = Mage::getModel('catalog/category')->getCategories($id);
    
    foreach ($children as $subcategory) {
          echo $subcategory->getName();
    }
    ?>
    

    你需要添加 Mage.php 并启动 Mage::app();

    <?php 
    require_once ('app/Mage.php');
    Mage::app();
    $children = Mage::getModel('catalog/category')->getCategories($id);
    
    foreach ($children as $subcategory) {
          echo $subcategory->getName();
    }
    ?>
    

    看起来您可能没有使用新控制器,因此您需要再次初始化应用程序。

    【讨论】:

    • 我添加了 mage.php 和星号 Mage::app();就像你说的,但我仍然收到 ajax 请求的错误消息
    • 这是我在 ajax 脚本中设置的消息“alert('error');”
    • 我试过 不工作,如果我删除 require_once ('app/Mage.php');法师::app();它有效
    • 您的解决方案非常有帮助,问题出在 url app/Mage.php 上,我用 require_once ($_SERVER['DOCUMENT_ROOT'] . '/app/Mage.php'); 解决了而不是 require_once ('app/Mage.php');
    猜你喜欢
    • 2014-02-04
    • 2012-11-03
    • 2013-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多