Magento ajax not working

My controller has this function in it:

public function saveLeadDaysAction(){
        echo "works";
    }

And in the phtml template I try:

jq("#prodcal_leaddays").on("blur", function(e){
        $saveurl="<?=$this->getUrl('prodcal/adminhtml_prodcaltab/saveLeadDays');?>";
        console.log('$saveurl');
        jq.post($saveurl,{'id':'test'},function(d){
            console.log(d);
        });
    });

The URL seems to return the entire page of the admin control panel, not the expected text works. However, if I open the URL directly in the browser, it displays the expected text.

What am I doing wrong?

Oh, and I tried adding ?isAjax=trueat the end of the url that returns:

{"error":true,"message":"Invalid Form Key. Please refresh the page."}

EDIT: Just clarify: - getUrlGenerates a URL, for example http://localhost/devmagento/index.php/prodcal/adminhtml_prodcaltab/saveLeadDays‌​/key/d3c92257c664d8d207f5a0bdeb3edebf/in the console. If I copy this URL from the console and paste it into the browser, it works as expected, and I get the text works. But when used with a jQuery post, it crashes and redirects to the control panel.

2: , post, , , GET, getUrl, , , ajax, javascript.

, :

jq("#prodcal_leaddays").on("blur", function(e){
        $saveurl="<?=$this->getUrl('prodcal/adminhtml_prodcaltab/saveLeadDays',array('id'=>'test'));?>";
        console.log($saveurl);
        jq.get($saveurl,function(d){
            console.log(d);
        });
    });
+4
2

POST- ajax, ? isAjax = 1 form_key. :

var values = {
    'form_key': "<?php echo Mage::getSingleton('core/session')->getFormKey(); ?>"
    // ...
};
$jq.post('<?php echo Mage::helper('adminhtml')->getUrl("..."); ?>?isAjax=1', values);
+2

URL, :

$this->getUrl('prodcal/adminhtml_prodcaltab/saveLeadDays')

, . , key, getUrl(), . key .

, . getUrl, /app/code/local ( community)/YourName/Prodcal/controllerlers/Adminhtml/ProdcaltabController.php

:

$array = array('works');    
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($array));
0

Source: https://habr.com/ru/post/1525450/


All Articles