0

I'm trying to call a controller located in Vendor/Module/Controller/Admin/CustomController/TestAction but I'm getting an unauthorized error. Below is my code

require(
    [
        'jquery',
        'mage/translate',
    ],
    function ($) {
      $("#generate_menu").on('click', function() {
        $.ajax({
            url : '/admin/vendor_module/CustomController/TestAction',
            type : 'GET',
            data: {
                format: 'json'
            },
            dataType:'json',
            success : function(data) {              
                alert('Data: '+data);
            },
            error : function(request,error)
            {
                alert("Error");
            }
        });
        return false;
      })
    }
);

Note: I don't have any .phtml defined because I'm calling this ajax from a js file and I just added a custom button in the catalog/category in the admin. I know my controller is working it's just that I'm getting an unauthorized access error.

Update: The JS file calling the Controller is in /app/code/Vendor/Module/view/adminhtml/web/js/mycustom.js. I have created a custom button that is located in the catalog->category just beside the save button. When I click this the expected behavior is it will trigger the ajax and the ajax will access the controller. But I get the unauthorized problem

  • I doubt that your controller works. the namespace is wrong. Vendor/Module/Controllers should be without the s at the end – Philipp Sander Aug 1 at 14:14
  • Sorry just a typo it's without an S – MadzQuestioning Aug 1 at 14:21
  • You should use getUrl() instead of hard code url – Sohel Rana Aug 1 at 14:25
  • from where exactly are you trying to call this javascript? you need to be authenticated as an admin! – Philipp Sander Aug 1 at 15:20
  • within the admin controller. I'll update my question @PhilippSander – MadzQuestioning Aug 1 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.