Skip to content

Call custom Admin Controller in magento 2 product edit giving an error, How to call admin controller in Ajax?

I have created a button in product admin image popup by overriding the gallery.phtml.

Created the custom js mixin to the product-gallery.js

requirejs-config.js (path – Ayakil/ProcessImage/view/adminhtml/requirejs-config.js) file contains like below.

var config = {
config: {
    mixins: {
        'Magento_Catalog/js/product-gallery': {
            'Ayakil_ProcessImage/js/product-gallery-mixin': true
        }
    }
}
};

product-gallery-mixin.js (path – Ayakil/ProcessImage/view/adminhtml/web/js/product-gallery-mixin.js) file contains like below.

define([
'jquery',
'underscore',
'mage/template',
'uiRegistry',
'mage/url'
], function ($, _, mageTemplate, registry, urlBuilder) {
'use strict';

var galleryWidgetMixin = {
  $dialog.on('click', '[data-role=background-remover]', $.proxy(function (e) {
            
            let imageUrl = $("#current-image-url").val();
            
            let apiUrl = urlBuilder.build('imgprocess/process/processimage');
            
            jQuery.ajax({
                showLoader: true, 
                url: apiUrl,
                data: {img:imageUrl},
                type: "POST", 
                dataType: 'json'
            }).done(function (data) { 
                alert(data); console.log(data); 
            });
        }, this));
        //------

        .................
    }
};
return function (targetWidget) {
    $.widget('mage.productGallery', targetWidget, galleryWidgetMixin);

    return $.mage.productGallery;
};
});

When i am clicking on the button i am getting a 404 error like below. Admin controller url is generating with the current edit url.

jquery.js:10109 POST https://magento245p1new.local/index.php/admin/catalog/product/edit/id/1414/key/226536366459bcf89b14af76496ced792f7b28d59dc365c3bc8dcbf528e64895/imgprocess/process/processimage/?isAjax=true 404 (Not Found)

How to give a proper controller url on this AJAX call? Kindly note this click event is triggering on the image click popup in product edit form area.