Hello!
I’m making an ajax query from a product page (code below). If I run it directly in browser, it works but if I run it inside a code (javascript) it redirects me back to the product page with a status 302 (picture attached).
It’s Magento 2.4.0.
How to make it work right so it won’t redirect me back to product page? Thanks in advance!
<?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;
}
}
---------------------
$.ajax({
url: baseUrl + 'module/ajax/fooaction',
type: "POST",
data: {},
showLoader: true,
cache: false,
crossDomain:true,
dataType: 'json',
success: function(response){
}
});Solved! Go to Solution.
SOLVED: It worked throught storage.post() ('mage/storage')
Hello @antoniogur615a