简体   繁体   中英

magento ajax isn't working

My controller has this function in it:

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

And in the template phtml I am trying to this:

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 admin dashboard page instead of the expected works text. However if I open the url directly in the browser, it does show the expected text.

What am I doing wrong?

Oh and I have tried appending ?isAjax=true at the end of the url, which returns:

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

EDIT: Just to clarify:- the getUrl generates a url like 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 in the browser, it works as expected and I get the works text. But when used with jQuery post, it fails and redirects to dashboard.

EDIT 2: It seems with post data, the key is wrong, cause if I change the above to use GET , and put the values within the getUrl function, it works properly, but the problem is since this is to be done by ajax, I need to change the values dynamically using javascript.

What I did for the get thing to work is:

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

You can use POST ajax requests but you must add ?isAjax=1 to the url and form_key to params. Example:

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

The URL generated by this code:

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

cannot be found, so you are redirected to the dashboard. If you try it manually, you don't have the key variable that is automatically added by getUrl() function, so you get that error. That key is a kind of session controller, and always changes.

To get to the right controller action, you have to check your file structure. As in your getUrl parameter, it shoul be / app / code / local (or community) / YourName / Prodcal / controllers / Adminhtml / ProdcaltabController.php

Return your response in this way:

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

我遇到了同样的问题,并通过直接将 form_key 添加到 url 来解决它:

$saveurl="<?=$this->getUrl('prodcal/adminhtml_prodcaltab/saveLeadDays');?>form_key=" + window.FORM_KEY;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM