1

I've set up the JS, and controller and checkout.html

I need to call the controller with ajax to getPrice() method. My controller name is GetAjax.php and here is the controller code.

<?php

namespace Iostpay\Iostpaymagento\Controller\Index;

use Magento\Framework\Controller\ResultFactory;

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

    public function execute()
    {
        print($this->getRequest()->getParam('customdata1'));
        print("heuss");
        die();
        /** @var \Magento\Framework\Controller\Result\Json $result */
        $result = $this->resultJsonFactory->create();

        return $result->setData(['success' => true]);
    }
}

And the JS code which triggers on click of place order.

require(["jquery"], function ($) {
    function transfer() {
        var customurl = "modulename/index/GetAjax'";
        $.ajax({
            url: customurl,
            type: 'POST',
            dataType: 'json',
            data: {
                customdata1: 'test1',
                customdata2: 'test2',
            },
            complete: function(response) {
                console.log( response );
            },
            error: function (xhr, status, errorThrown) {
                console.log('Error happens. Try again.');
            }
        });
   }
});

The Error I get in console is

POST http://127.0.0.1/magento3/newmodule/GetAjax/execute 404 (Not Found)

How can I execute any specific method from the controller.

1 Answer 1

1

Update Your Js Code with following.

 require(["jquery", "mage/url"],function($,urlBuilder) 
 {
    function transfer()
    {
        var customurl = urlBuilder.build('modulename/index/GetAjax');

        $.ajax({
            url: customurl,
            type: 'POST',
            dataType: 'json',
            data: {
                customdata1: 'test1',
                customdata2: 'test2',
            },
            complete: function(response) {             

                console.log( response );   
            },
            error: function (xhr, status, errorThrown) {
                console.log('Error happens. Try again.');
            }
        });
    }

});
12
  • I tried with the same JS. I get this error index:376 Uncaught ReferenceError: transfer22 is not defined at HTMLButtonElement.onclick (index:376) onclick @ index:376 jquery.js:10260 POST http://127.0.0.1/magento3/newmodule/index/newmodule/index/GetAjax' 404 (Not Found) Commented Nov 8, 2019 at 10:13
  • Am i missing some thing?? Do i have to define the controller also? Commented Nov 8, 2019 at 10:22
  • add html code from where you are trying to fire this function @Garry Commented Nov 8, 2019 at 10:26
  • Thanks. I have found the bug. Commented Nov 8, 2019 at 10:33
  • One more thing i want to know, can we create the ajax URL without using this function <?php echo $block->getUrl() ?>. As i want to use the jquery in js file. Thanks Commented Nov 8, 2019 at 10:36

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.