Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento adminhtml AJAX query returns 302 status

I am currently developing a Magento extension whose main components are a frontend widget and a backend admin options panel. I have no frontend controller, but I have an adminhtml controller that is giving me some trouble. Here is some of the code in my controller.

File: /app/code/community/Mynamespace/Myextension/controllers/Adminhtml/MybackendController.php

class Mynamespace_Myextension_Adminhtml_MybackendController
    extends Mage_adminhtml_Controller_Action
{
    protected function normalAction() {
    }

    protected function ajaxAction() {
        die('got here');
    }
}

normalAction is an action that is called by navigating through links such as http://mystore.com/index.php/admin/mybackend/normal/key/.../ . There are a few actions like this such as index, save, edit, grid, etc. The entry point to my controller is a menu entry that leads to the index action. All such actions work as expected.

ajaxAction is an action that is called only through an AJAX POST query.

The problem is that whenever I attempt to do this, I get a 302 HTTP status code reply that redirects to the admin dashboard (admin/index/index). If I try to access this action directly by typing its link in the browser, I still get a 302. If I add a menu entry to this action, then it works as expected and I see the "got here" text.

The Javascript/jQuery code that does the AJAX request is not complex:

$.post(
    'http://mystore.com/index.php/admin/mybackend/ajax/key/.../',
    somePostDataObject,
    function() {
        alert( 'success' );
    }
);

Note that the success function gets called even though the status code is 302.

I tried debugging the routing mechanism to understand exactly what causes it to redirect. So far I have deduced that a problem occurs in the preDispatch method of the Mage_Core_Controller_Varien_Action class. The module, controller and action are successfully resolved and the request is marked as dispatched (_dispatched property is true) until the controller_action_predispatch event is dispatched. After the event is resolved, the request appears as not being dispatched (_dispatched becomes false). Presumably one of the objects that listen for this event does some further processing on the request and rejects it. I have not been able to debug any deeper than this.

Normally I would post some code to help you understand what's going on even better, but there are quite a lot of lines to copy from the various configuration files and classes, so I will do that as people write comments or answers asking to see specific files. So far, I believe the configuration is correct, as all the other functionality besides the AJAX request is working ok.

like image 624
Grampa Avatar asked May 01 '13 14:05

Grampa


1 Answers

The Url should be http://mystore.com/index.php/admin/mybackend/ajax/key/.../?isAjax=true, so magento knows this is an ajax request.

Also you need to provide a from_key in your POST data which is stored in the global JavaScript Variable window.FORM_KEY.

Your jQuery Request should look something like this:

function magentoAdminAjax(data, callback) {
    data.form_key = window.FORM_KEY;
    $.post(
        'http://mystore.com/index.php/admin/mybackend/ajax/key/.../?isAjax=true',
        data,
        callback
    );
}

The Code for this check can be found in Mage_Adminhtml_Controller_Action::preDispatch(Line: 164) (magento-1.7.0.2)

like image 164
Handfeger Avatar answered Sep 17 '22 18:09

Handfeger