1

Hi there I'm trying to run a custom module when a product is saved at the moment I have an event listener on the product page and need to send some of the info entered to a controller using ajax.

When making the Ajax call the result is coming back with the current pages HTML on the console. Here is my code any help would be awesome thanks :)

RelatedProducts/Items/view/adminhtml/templates/js.phtml

            function ajax(){
                var name = document.getElementsByName("product[name]")[0].value;
                var type = document.getElementsByName("product[product_category_type]")[0].value;

                $.ajax({
                    url: "RelatedProducts_Items/Adminhtml/CustomAjax", 
                    data: { name : name, type : type},
                    type: "POST",
                    success: function(result){
                        console.log(result);
                    } 
                });
            }

RelatedProducts/Items/etc/adminhtml/routes.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="admin">
        <route id="RelatedProducts" frontName="RelatedProducts">
            <module name="RelatedProducts_Items"/>
        </route>
    </router>
</config>

RelatedProducts/Items/Controller/Adminhtml/CustomAjax.php

namespace RelatedProducts\Items\Controller\Adminhtml;

class CustomAjax extends \Magento\Backend\App\Action


    public function execute()
    {

       echo "testing";
    }
}
1
  • You should accept an answer to help other developers to find their solutions. Commented Jun 11, 2017 at 14:08

1 Answer 1

2

Follow This Step

app/code/Relatedproducts/Items/etc/adminhtml/routes.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="admin">
        <route frontName="relatedproducts_items" id="relatedproducts_items">
            <module before="Magento_Backend" name="RelatedProducts_Items"/>
        </route>
    </router>
</config>

app/code/RelatedProducts/Items/view/adminhtml/templates/js.phtml

function ajax(){
    var name = document.getElementsByName("product[name]")[0].value;
    var type = document.getElementsByName("product[product_category_type]")[0].value;

    $.ajax({
        url: 'admin/relatedproducts_items/customajax',
        showLoader: true,
        data: {id:<?php echo $id ?>,p:pincode},
        type: "POST",
        success: function(result){
            console.log(result);
        }
    });
}

RelatedProducts/Items/Controller/Adminhtml/CustomAjax.php

<?php

namespace RelatedProducts\Items\Controller\Adminhtml;

class CustomAjax extends \Magento\Backend\App\Action
{
    protected $resultPageFactory;

    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\Controller\Result\JsonFactory $resultPageFactory
    ) {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
    }

    public function execute()
    {
        if($this->getRequest()->isAjax()){

            echo "Testing";
        }

        return false;
    }
}
4
  • Echo $id is giving me an error on js.phtml so I tried using the data that Im trying to send (data: { name : name, type : type}) This had no luck too same result just consoles the page html and doesn't return Testing Commented May 15, 2017 at 4:02
  • @NathanielMeyer Please check your frontend router working or not by echo "testing". Commented May 15, 2017 at 4:43
  • When calling the frontend controller on front end of the website I get testing When calling the front end controller from the backend I get the page html again. This is the same when calling the backend controller. Commented May 15, 2017 at 5:24
  • Please can you explain where is the admin coming from the url -> url: 'admin/relatedproducts_items/customajax', Commented Dec 16, 2019 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.