Question

I´m sending a POST request with data via XMLHttpRequest. The response that I´m getting do have the status code 302.

Javascript function:

function createCSV() {
let xhttp = new XMLHttpRequest();
xhttp.open("POST", '', false);
xhttp.setRequestHeader("Content-type", "application/json");
xhttp.send(JSON.stringify(formDataObject));}

Response:

Request URL: http://localhost:8888/magento2/delivery/inputform/national
Request Method: POST
Status Code: 302 Found
Remote Address: [::1]:8888
Referrer Policy: no-referrer-when-downgrade

enter image description here

LAYOUT

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <!--Include CSS-->
        <css src="Magento_VersandForm::css/inputform.css" />

        <script src="Magento_VersandForm::js/inputform.js"/>
        <script src="Magento_VersandForm::js/w3.js"/>
    </head>
    <body>
        <referenceContainer name="content">
            <block class="Magento\VersandForm\Block\Send" name="Inputform_send" template="Magento_VersandForm::national_input.phtml" />
        </referenceContainer>
    </body>
</page>

This is my BLOCK

<?php
 namespace Magento\VersandForm\Block;

 class Send extends \Magento\Framework\View\Element\Template
 {
   public function 
   __construct(\Magento\Framework\View\Element\Template\Context 
   $context)
{
    parent::__construct($context);
}

/**
 * Returns description for Tooltip
 * @return string
 */
public function getRequiredFieldsDescription(){
    return 'Diese Felder müssen ausgefüllt sein';
}

}

This is my CONTROLLER

<?php
namespace Magento\VersandForm\Controller\Inputform;

use Magento\Framework\Controller\ResultFactory;

class National extends \Magento\Framework\App\Action\Action
{
    protected $_pageFactory;
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $pageFactory)
    {
        $this->_pageFactory = $pageFactory;
        return parent::__construct($context);
    }

    /**
     * Booking action
     *
     * @return void
     */
    public function execute()
    {
        // 1. POST request : Get Data From Javascript
        $post = (array) $this->getRequest()->getPost();
    }
}

Why does Magento want to redirect me. Please provide Information if you have excperience with this kind of error.

Was it helpful?

Solution

Found the Solution:

The problem was not in the Controller. The problem was the request I sent with my Javascript. It seems like Magento needs the Header " X-Requested-With: XMLHttpRequest".So just add it to your request

function createCSV() {
let xhttp = new XMLHttpRequest();
xhttp.open("POST", '', false);
xhttp.setRequestHeader("Content-type", "application/json");
xhttp.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xhttp.send(JSON.stringify(formDataObject));}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top