【问题标题】:Magento call custom php code through jquery ajaxMagento通过jquery ajax调用自定义php代码
【发布时间】:2016-05-13 16:50:19
【问题描述】:

您好,我是 magento 的新手,我正在尝试从前端通过 jquery ajax 调用简单的自定义 php。我在谷歌搜索他们给出了创建自定义模块的答案,然后你可以通过 jquery ajax 调用 php。我已尝试以下步骤是我尝试过的以下步骤请检查并告诉我我的错误是什么

<script>
                        var jq = jQuery.noConflict();
                        jq(document).ready(function(){
                            jq(".listing_duplicate" ).on( "click", function() {
                                var id= this.id;
                            var uniqu_number = id.split("_");

                            var unique  = uniqu_number[3];

jq.ajax({
            type: "POST",
            dataType: "JSON",
            data :{'value':unique},
            url :"<?php echo $this->getUrl('customajax/ajax/index'); ?>",
            success:function(data){
                alert(data);

              }
        });
         });
          });
</script>

上面是我调用ajax的phtml文件

我在下面的路径中创建了自定义模块 我创建了xxx.com\app\code\local\Custom\Customajax\etc\config.xml

<?xml version="1.0"?>
<config>
  <modules>
    <ThomasRyan_Checkoutajax>
      <version>0.1.0</version>
    </ThomasRyan_Checkoutajax>
  </modules>
  <frontend>
  <routers>
          <customajax>
            <use>standard</use>
            <args>
              <module>Custom_Customajax</module>
              <frontName>customajax</frontName>
            </args>
          </customajax>
       </routers>

    <layout>
     <updates>
       <checkoutajax>
         <file>checkoutajax.xml</file>
       </checkoutajax>
     </updates>
   </layout>
 </frontend>
</config>

在这条路径中我创建了控制器xxx.com\app\code\local\Custom\Customajax\controllers\AjaxController.php

<?php

class ThomasRyan_Checkoutajax_AjaxController extends Mage_Core_Controller_Front_Action {

     public function indexAction() {
        echo 'Hello Index!';
    }
}

在这条路径中我创建了块xxx.com\app\code\local\Custom\Customajax\block\Customajax.php

<?php
class Custom_Customajax_Block_Customajax extends Mage_Core_Block_Template
{
    echo "teswtme";
}
?>

最后激活我添加的模块xxx.com\app\etc\config.xml

<blocks>
        <custom_customajax>
            <class>Custom_Customajax_Block</class>
        </custom_customajax>
    </blocks>

xxx.com\app\etc\modules\ThomasRyan_Checkoutajax.xml

<?xml version="1.0"?>
<config>
    <modules>
     <ThomasRyan_Checkoutajax>
            <active>true</active>
            <codePool>local</codePool>
        </ThomasRyan_Checkoutajax>
    </modules>
</config>

请告诉我在为我调用 jquery ajax 时出错的地方,它显示此错误未找到为什么它显示以下错误是我的错误。

POST http://localhost/xxx.com/index.php/customajax/ajax/index/ 404 (Not找到)

【问题讨论】:

    标签: jquery ajax magento-1.8


    【解决方案1】:

    你已经创建了像 Custom > Yourmodule > files 这样的文件夹,并且在 config 和其他任何地方你都将它定义为 ThomasRyan_Checkoutajax 这就是你出错的地方。它应该像您的代码一样 Custom_Checkoutajax

    <?xml version="1.0"?>
    <config>
    <modules>
     <ThomasRyan_Checkoutajax>
            <active>true</active>
            <codePool>local</codePool>
        </ThomasRyan_Checkoutajax>
        </modules>
    </config>
    

    应该是

    <?xml version="1.0"?>
    <config>
     <modules>
       <Custom_Checkoutajax>
            <active>true</active>
            <codePool>local</codePool>
        </Custom_Checkoutajax>
     </modules>
    </config>
    

    【讨论】:

    • 好的,我的问题是我应该在哪里更改名称 custom_checkout ajax?
    • 我已经提到过.. 在所有插件文件中将 ThomasRyan_Checkoutajax 替换为 Custom_Checkoutajax。我已经编辑了我的答案检查。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-09
    • 1970-01-01
    • 2015-07-22
    相关资源
    最近更新 更多