如何使用jquery ajax magento调用控制器

时间:2014-03-28 10:41:02

标签: ajax magento

我已经在magento中创建了自定义模块,我想使用jquery ajax访问控制器,但我找不到正确的方法来执行此操作。
模板中的Ajax代码是

 jQuery(".deleteAttrKeyId").on("click",function(){
      var baseUrl="<?php echo Mage::getBaseUrl();?>";
      var idArr=this.id.split("-");
      attrKeyId=idArr[1];
      alert(this.id);
      jQuery.ajax({
        type: "POST",
        dataType: "JSON",
        data :{'id':attrKeyId},
        url :baseUrl+"groupprice/adminhtml_grouppricebackend/index",
        complete:function(){alert("completed");
          },
        success:function(result){
          alert(result);
          }

        });
    });

布局中的分组代码是

<groupprice_adminhtml_grouppricebackend_index>
<reference name="content">
<block type="groupprice/adminhtml_grouppricebackend" name="grouppricebackend"  template="groupprice/grouppricebackend.phtml"/>
</reference>
</groupprice_adminhtml_grouppricebackend_index>

控制器代码

<?php
class Group_GroupPrice_Adminhtml_GrouppricebackendController extends     Mage_Adminhtml_Controller_Action
{
public function indexAction()
{      
 echo  __FILE__;
}
}

2 个答案:

答案 0 :(得分:11)

使用此

<script type="text/javascript">
function callController(){
       new Ajax.Request("<?php echo   $this->getUrl('groupprice/adminhtml_grouppricebackend/index') ?>", {
           method: 'Post',
           parameters: {"parameter_name":"value"},
           onComplete: function(transport) {

               alert(transport.responseText);

           }
       });
   }

答案 1 :(得分:8)

在phtml中添加此代码

<button type="button" onclick="callController()" title="submit" class="button">submit</button>

<script type="text/javascript">
 function callController(){
           new Ajax.Request("<?php echo $this->getUrl('module/controller/action') ?>", {
               method: 'Post',
               parameters: {parameter_name:value},
               onComplete: function(transport) {

                   alert(transport.responseText);

               }
           });
       }
</script>