4

How Do I create my own Action in Controller to call url from Ajax in Magento 2.0.

Like in Magento 1.9 we can create any Action an any Controller and call that from URL.

1 Answer 1

13

You can use the code below your own Action in controller to call url from Ajax. Using phtml file.

In your (Like vendor/module/Block/Example.php) block put below code.

public function getAjaxUrl(){
    return $this->getUrl("module/index/view"); // Controller Url
}

Put below code view/fronted/templates/example.phtml

<script>
    require(
        ['jquery','example'],
        function($,example){

        var ajaxurl = '<?php echo $block->getAjaxUrl() ?>';
        example.exampleData(ajaxurl);

    }); 
</script>

Put below code view/fronted/web/js/example.js

define([
        'jquery',
        ],
    function($,example){

    return {
        exampleData:function(ajaxurl){
        $(document).on('click','.example-list',function (event){
                event.preventDefault();
                var id=$(this).attr('id');
                $.ajax({
                    url:ajaxurl,
                    type:'POST',
                    showLoader: true,
                    dataType:'json',
                    data: {id:id},                                      
                    success:function(response){
                        //put your code
                    }
                });
            });
        }
    }

});

Your own controller like vendor/module/Controller/Index/View.php

<?php

namespace Vendor\Module\Controller\Index;

use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\Action\Context;
use Vendor\Module\Model\ResourceModel\Example\CollectionFactory;

class View extends \Magento\Framework\App\Action\Action
{

    protected $resultPageFactory;

    public function __construct(
        Context $context,
        CollectionFactory $exampleFactory,
        PageFactory $resultPageFactory
    ) {
        $this->resultPageFactory = $resultPageFactory;
        $this->exampleFactory = $exampleFactory;
        parent::__construct($context);
    }
    /**
     * Index action
     *
     * @return $this
     */
    public function execute()
    {
        /*Put below your code*/
        $id = $this->getRequest()->getParam('id', false);
        $responseData = $this->exampleFactory->create();

        $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
        $resultJson->setData($responseData);
        return $resultJson;
    }
}
4
  • 1
    Controller Url (module/index/view) we have to create view controller with with execute method OR we can define my own method ? Commented Aug 8, 2016 at 7:59
  • Controller Url(module/index/view) in this view means controller action. Commented Aug 9, 2016 at 8:38
  • Hii, Ashish.. Now check above edited code for your understanding. I put code with controller action.. Commented Aug 9, 2016 at 8:54
  • Refused to execute script from '....' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled. Cannot execute custom js file in Magento 2.1.8
    – Gediminas
    Commented Apr 17, 2018 at 13:14

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.