C
C
camradee2020-10-28 16:54:15
Magento
camradee, 2020-10-28 16:54:15

How to make an ajax request in Magento 2.4?

It is necessary to execute an ajax request from the product page. The Internet is full of examples of how to do this for older versions of Magento, there are options from my custom page, I try by analogy, it doesn’t work - if through the address bar of the browser - everything works, if from javascript - no. The controller's constructor fires, but the execute() method is not called, ajax itself returns with a 302 status, after which the product page is called again5f997528b73bd385306102.png

$.ajax({
                        url: baseUrl + 'module/ajax/fooaction',
                        type: "POST",
                        data: {},
                        showLoader: true,
                        cache: false,
                        crossDomain:true,
                        dataType: 'json',
                        success: function(response){
                        }
                    });


The controller code is something like this
<?php
namespace Vendor\Module\Controller\Ajax;

use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\App\Action\HttpGetActionInterface;

class FooAction implements HttpGetActionInterface
{
    /**
     * @var \Magento\Framework\App\Action\Contex
     */
    private $context;
    /**
     * @var \Magento\Framework\Controller\Result\JsonFactory
     */
    protected $resultJsonFactory;

    /**
     * @param \Magento\Framework\App\Action\Context $context
     * @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
     */
    public function __construct(Context $context, JsonFactory $resultJsonFactory)
    {
        $this->context = $context;
        $this->resultJsonFactory = $resultJsonFactory;
    }

    /**
     * @return json
     */
    public function execute()
    {
        $params = $this->context->getRequest()->getParams();     
        $resultJson = $this->resultJsonFactory->create();
        $resultJson->setData(['message' => 'Hello world', 'success' => true]);
        return $resultJson;
    }
}


Why approximately, because this is already its 10-20 iteration, I tried different options, right now this is:
...............
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\Action\Action;

class FooAction extends Action implements HttpGetActionInterface, HttpPostActionInterface
{
    /**
     * @var \Magento\Framework\App\Action\Contex
     */
    private $context;
    /**
     * @var \Magento\Framework\Controller\Result\JsonFactory
     */
    protected $resultJsonFactory;

    /**
     * @param \Magento\Framework\App\Action\Context $context
     * @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
     */
    public function __construct(Context $context, JsonFactory $resultJsonFactory)
    {
        $this->context = $context;
        $this->resultJsonFactory = $resultJsonFactory;
        parent::__construct($context);
    }
............................................................


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

What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question