3

I have created custom module in magento, I want to access controller using jquery ajax but I am not finding correct method for doing this.
Ajax code in template is

 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);
          }

        });
    });

Block code in layout is

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

Controller code is

<?php
class Group_GroupPrice_Adminhtml_GrouppricebackendController extends     Mage_Adminhtml_Controller_Action
{
public function indexAction()
{      
 echo  __FILE__;
}
}
Ashwani Panwar
  • 3,819
  • 3
  • 46
  • 66

2 Answers2

13

use this

<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);

           }
       });
   }

9

add this code in 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>
Shivam
  • 2,443
  • 17
  • 31
  • for your reference see also http://stackoverflow.com/questions/16920083/magento-jquery-ajax-how-do-i-reload-just-parts-of-my-custom-module-instead – Shivam Mar 28 '14 at 11:03