我在admin中创建了名为 createadmincontroller 模块的自定义模块,它配置了config.xml.i想要从.phtml调用带有ajax的控制器索引函数,但是它会抛出错误&# 34; 未捕获的TypeError:undefined不是函数" 谁能告诉我哪里出错了?以下是我的完整模块详情:
我的代码是:
JR-> CreateAdminController-> controllers-> Adminhtml-> CustomController.php
<?php
class JR_CreateAdminController_Adminhtml_CustomController extends Mage_Adminhtml_Controller_Action
{
public function indexAction()
{
if(isset($_POST['data'])){
echo 'successful';
//exit;
}
$this->loadLayout()
->_setActiveMenu('mycustomtab')
->_title($this->__('Index Action'));
$this->renderLayout();
}
?>
JR-&GT; CreateAdminController-&GT; etc-&GT; config.xml中
<?xml version="1.0"?>
<config>
<modules>
<JR_CreateAdminController>
<version>1.0.0</version>
</JR_CreateAdminController>
</modules>
<global>
<helpers>
<jr_createadmincontroller>
<!-- Helper definition needed by Magento -->
<class>Mage_Core_Helper</class>
</jr_createadmincontroller>
</helpers>
</global>
<admin>
<routers>
<adminhtml>
<args>
<modules>
<jr_createadmincontroller before="Mage_Adminhtml">JR_CreateAdminController_Adminhtml</jr_createadmincontroller>
</modules>
</args>
</adminhtml>
</routers>
</admin>
<adminhtml>
<layout>
<updates>
<petra>
<file>createadmincontroller.xml</file>
</petra>
</updates>
</layout>
</adminhtml>
APP-&GT;设计 - &GT; adminhtml-&GT;默认 - &GT;默认 - &GT;&版图GT; createadmincontroller.xml
<?xml version="1.0"?>
<layout version="0.1.0">
<adminhtml_custom_index>
<reference name="content">
<block type="adminhtml/template" name="createadmincontroller" template="createadmincontroller/index.phtml" />
</reference>
</adminhtml_custom_index>
</layout>
APP-&GT;设计 - &GT; adminhtml-&GT;默认 - - GT;默认 - - GT;模板 - &GT; createadmincontroller-&GT; index.phtml
<button type="button" class="scalable" onclick="test()">Click Me!</button>
<script>
function test(){
alert("wao");
var t = '<?php echo Mage::getUrl('*/custom'); ?>';
$.ajax({
url: "<?php echo $this->getUrl('*/custom/'); ?>"
}).done(function() {
alert("Hey");
});
}
</script>
JR-&GT; CreateAdminController-&GT; Helper-&GT; Data.php
<?php
class JR_CreateAdminController_Helper_Data extends Mage_Core_Helper_Abstract
{}
?>
答案 0 :(得分:3)
您是否尝试使用jQuery.ajax()
代替$.ajax()
?
Magento使用Prototype JS,它已经声明了$
全局变量。因此,您需要使用jQuery in no-conflict模式并改为使用jQuery
。
<强>替代强>
$j.ajax()
申请无冲突模式后使用var $j = jQuery.noConflict();
(感谢@ b.enoit.be) (function($){ .... $.ajax() .... })(jQuery);
(另请参见此处JavaScript / jQuery closure function syntax) 将jQuery添加到Magento
更改布局XML文件createadmincontroller.xml
:
<?xml version="1.0"?>
<layout version="0.1.0">
<adminhtml_custom_index>
<reference name="head">
<action method="addItem">
<type>skin_js</type>
<script>js/jquery-1.x.x.js</script>
</action>
<block type="core/text" name="jquery.noconflict">
<action method="setText">
<text><![CDATA[<script type="text/javascript">var $j = jQuery.noConflict();</script>]]>
</text>
</action>
</block>
</reference>
<reference name="content">
<block type="adminhtml/template" name="createadmincontroller" template="createadmincontroller/index.phtml" />
</reference>
</adminhtml_custom_index>
</layout>