How to use ajax in magento backend

why is the ajax call from magento backend (from .phtml) redirecting to magento dashboard even though you are submitting the form correctly with the form? please, help.

in the ajax call:

var dataRecord = j('#newForm').serialize(); 
    var url = "<?php echo $this->getUrl('*/*/addNewColumn') ?>";

    j.ajax({
    type: "POST",
    url: url,
    data: {data1: dataRecord}
    })
    .done(function( msg ) {
        alert(msg);
    });

      

+3


source to share


2 answers


It worked when I sent the form_key to the "data" of the ajax call.



var dataRecord = jQuery('#newForm').serialize(); 
var url = "<?php echo $this->getUrl('*/*/addNewColumn') ?>";
<?php $k = Mage::getSingleton('core/session')->getFormKey(); ?>

jQuery.ajax({
type: "POST",
url: url,
data: {data1: dataRecord,form_key:'<?php echo $k ?>'}
})
.done(function( msg ) {
    alert(msg);
});

      

+7


source


Below is the code for ajax in admin panel.



jQuery(".btn_save_email").click(function(){
        var data1val = jQuery('.data1val').val();
        var data2val = jQuery('.data2val').val();
        url = '<?php echo $this->getUrl('moduleName/adminhtml_controllerName/functionName') ?>';             
        new Ajax.Request(url, {
        parameters: {isAjax: 1, method: 'POST',data1:data1val,data2:data2val},
        onSuccess: function(transport) {
           jQuery('.class').html(transport['responseText']);
        }
       }); 
    }); 

      

+2


source







All Articles