How to create AJAX action for POST requests in Magento 2.3?How to call a model method from controller in Magento2I created a custom module ,but getting error, not able to figure out what the error is about. How to get out of this error?Magento 2.1: Invoke urlBuilder->getUrl() in a controller in a custom moduleCalling another controller and getting a responseMagentoFrameworkObjectManagerObjectManager givenMagento 2 : Overriding Default Contact Controller POST ActionI have created an extension to show Customer Company Name in Order grid. But when creating new order, order is not showing in order gridMagento 2 Create dynamic array From different Model Collection to use in multi select in gridMagento 2.3 Can't view module's front end page output?Magento 2.3 - Get child categories of parent

What is the word?

TV show starring two men who develop various gadgets

Does knowing the surface area of all faces uniquely determine a tetrahedron?

I have found ports on my Samsung smart tv running a display service. What can I do with it?

Using roof rails to set up hammock

Print the new site header

Would a 7805 5v regulator drain a 9v battery?

Are there examples of rowers who also fought?

How to avoid offending original culture when making conculture inspired from original

Derivation of CDF of a function that results in an exponential distribution

Right indicator flash-frequency has increased and rear-right bulb is out

Why was New Asgard established at this place?

Definition of 'vrit'

Does cooling a potato change the nature of its carbohydrates?

How is linear momentum conserved in circular motion?

Why swap space doesn't get filesystem check at boot time?

What is this plant I saw for sale at a Romanian farmer's market?

I'm yearning in grey

Regex version of strip() - Ch. 7 Automate the Boring Stuff

How to make a villain when your PCs are villains?

How did the European Union reach the figure of 3% as a maximum allowed deficit?

What does this Swiss black on yellow rectangular traffic sign with a symbol looking like a dart mean?

What is the context for Napoleon's quote "[the Austrians] did not know the value of five minutes"?

My student in one course asks for paid tutoring in another course. Appropriate?



How to create AJAX action for POST requests in Magento 2.3?


How to call a model method from controller in Magento2I created a custom module ,but getting error, not able to figure out what the error is about. How to get out of this error?Magento 2.1: Invoke urlBuilder->getUrl() in a controller in a custom moduleCalling another controller and getting a responseMagentoFrameworkObjectManagerObjectManager givenMagento 2 : Overriding Default Contact Controller POST ActionI have created an extension to show Customer Company Name in Order grid. But when creating new order, order is not showing in order gridMagento 2 Create dynamic array From different Model Collection to use in multi select in gridMagento 2.3 Can't view module's front end page output?Magento 2.3 - Get child categories of parent






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















How to create a controller action that can be requested by POST properly in own custom payment module in Magento 2.3? How to create a parent class that will be extended by classes corresponding to controller actions which can be requested by POST properly in this version of Magento? I have created such a parent class:



<?php
namespace BillonPaymentController;
/**
* Ajax source
*/
class AjaxAction extends MagentoFrameworkAppActionAction

public function __construct(MagentoFrameworkAppActionContext $context)

return parent::__construct($context);

public function execute()

//(...)




The classes of the controller actions looks like this:



<?php
namespace BillonPaymentControllerMultishipping;
/**
* Ajax source that initiate payment in multishipping checkout
*/
class Initiation extends BillonPaymentControllerAjaxAction

/**
* Initiates the payment in multishipping checkout
*/
public function execute()

parent::execute();
//(...)




Such actions can be request in Magento 2.0 - 2.2 by POST (above action by '(store address)/billon/multishipping/initiation'). However I can send only GET request properly to my controller actions in Magento 2.3. After I send POST request to my action, the action redirects to the referrer with HTTP code 302. I found a ugly solution in the parent class:



<?php
namespace BillonPaymentController;
/**
* Ajax source
*/
class AjaxAction extends MagentoFrameworkAppActionAction

public function __construct()

$this->execute();
die();

public function execute()

//(...)




This solution disables the translation with a function '__'. How to resolve this problem? How to disable the redirection of POST requests from the controller actions to the referrers in Magento 2.3 in proper way? How should I change my action classes to receive POST request? Could you help me?










share|improve this question




























    0















    How to create a controller action that can be requested by POST properly in own custom payment module in Magento 2.3? How to create a parent class that will be extended by classes corresponding to controller actions which can be requested by POST properly in this version of Magento? I have created such a parent class:



    <?php
    namespace BillonPaymentController;
    /**
    * Ajax source
    */
    class AjaxAction extends MagentoFrameworkAppActionAction

    public function __construct(MagentoFrameworkAppActionContext $context)

    return parent::__construct($context);

    public function execute()

    //(...)




    The classes of the controller actions looks like this:



    <?php
    namespace BillonPaymentControllerMultishipping;
    /**
    * Ajax source that initiate payment in multishipping checkout
    */
    class Initiation extends BillonPaymentControllerAjaxAction

    /**
    * Initiates the payment in multishipping checkout
    */
    public function execute()

    parent::execute();
    //(...)




    Such actions can be request in Magento 2.0 - 2.2 by POST (above action by '(store address)/billon/multishipping/initiation'). However I can send only GET request properly to my controller actions in Magento 2.3. After I send POST request to my action, the action redirects to the referrer with HTTP code 302. I found a ugly solution in the parent class:



    <?php
    namespace BillonPaymentController;
    /**
    * Ajax source
    */
    class AjaxAction extends MagentoFrameworkAppActionAction

    public function __construct()

    $this->execute();
    die();

    public function execute()

    //(...)




    This solution disables the translation with a function '__'. How to resolve this problem? How to disable the redirection of POST requests from the controller actions to the referrers in Magento 2.3 in proper way? How should I change my action classes to receive POST request? Could you help me?










    share|improve this question
























      0












      0








      0


      1






      How to create a controller action that can be requested by POST properly in own custom payment module in Magento 2.3? How to create a parent class that will be extended by classes corresponding to controller actions which can be requested by POST properly in this version of Magento? I have created such a parent class:



      <?php
      namespace BillonPaymentController;
      /**
      * Ajax source
      */
      class AjaxAction extends MagentoFrameworkAppActionAction

      public function __construct(MagentoFrameworkAppActionContext $context)

      return parent::__construct($context);

      public function execute()

      //(...)




      The classes of the controller actions looks like this:



      <?php
      namespace BillonPaymentControllerMultishipping;
      /**
      * Ajax source that initiate payment in multishipping checkout
      */
      class Initiation extends BillonPaymentControllerAjaxAction

      /**
      * Initiates the payment in multishipping checkout
      */
      public function execute()

      parent::execute();
      //(...)




      Such actions can be request in Magento 2.0 - 2.2 by POST (above action by '(store address)/billon/multishipping/initiation'). However I can send only GET request properly to my controller actions in Magento 2.3. After I send POST request to my action, the action redirects to the referrer with HTTP code 302. I found a ugly solution in the parent class:



      <?php
      namespace BillonPaymentController;
      /**
      * Ajax source
      */
      class AjaxAction extends MagentoFrameworkAppActionAction

      public function __construct()

      $this->execute();
      die();

      public function execute()

      //(...)




      This solution disables the translation with a function '__'. How to resolve this problem? How to disable the redirection of POST requests from the controller actions to the referrers in Magento 2.3 in proper way? How should I change my action classes to receive POST request? Could you help me?










      share|improve this question














      How to create a controller action that can be requested by POST properly in own custom payment module in Magento 2.3? How to create a parent class that will be extended by classes corresponding to controller actions which can be requested by POST properly in this version of Magento? I have created such a parent class:



      <?php
      namespace BillonPaymentController;
      /**
      * Ajax source
      */
      class AjaxAction extends MagentoFrameworkAppActionAction

      public function __construct(MagentoFrameworkAppActionContext $context)

      return parent::__construct($context);

      public function execute()

      //(...)




      The classes of the controller actions looks like this:



      <?php
      namespace BillonPaymentControllerMultishipping;
      /**
      * Ajax source that initiate payment in multishipping checkout
      */
      class Initiation extends BillonPaymentControllerAjaxAction

      /**
      * Initiates the payment in multishipping checkout
      */
      public function execute()

      parent::execute();
      //(...)




      Such actions can be request in Magento 2.0 - 2.2 by POST (above action by '(store address)/billon/multishipping/initiation'). However I can send only GET request properly to my controller actions in Magento 2.3. After I send POST request to my action, the action redirects to the referrer with HTTP code 302. I found a ugly solution in the parent class:



      <?php
      namespace BillonPaymentController;
      /**
      * Ajax source
      */
      class AjaxAction extends MagentoFrameworkAppActionAction

      public function __construct()

      $this->execute();
      die();

      public function execute()

      //(...)




      This solution disables the translation with a function '__'. How to resolve this problem? How to disable the redirection of POST requests from the controller actions to the referrers in Magento 2.3 in proper way? How should I change my action classes to receive POST request? Could you help me?







      magento2 controllers ajax magento2.3 routing






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 9 at 12:41









      Kamil SzmitKamil Szmit

      32




      32




















          3 Answers
          3






          active

          oldest

          votes


















          0














          To create AJAX action for POST requests in Magento 2.3, Please see the code below.



          namespace BillonPaymentControllerMultishipping;

          class Initiation extends MagentoFrameworkAppActionAction


          protected $resultPageFactory;
          protected $jsonHelper;

          /**
          * Constructor
          *
          * @param MagentoFrameworkAppActionContext $context
          * @param MagentoFrameworkJsonHelperData $jsonHelper
          */
          public function __construct(
          MagentoFrameworkAppActionContext $context,
          MagentoFrameworkViewResultPageFactory $resultPageFactory,
          MagentoFrameworkJsonHelperData $jsonHelper
          )
          $this->resultPageFactory = $resultPageFactory;
          $this->jsonHelper = $jsonHelper;
          parent::__construct($context);


          /**
          * Execute view action
          *
          * @return MagentoFrameworkControllerResultInterface
          */
          public function execute()

          try
          return $this->jsonResponse('your response');
          catch (MagentoFrameworkExceptionLocalizedException $e)
          return $this->jsonResponse($e->getMessage());
          catch (Exception $e)
          $this->logger->critical($e);
          return $this->jsonResponse($e->getMessage());



          /**
          * Create json response
          *
          * @return MagentoFrameworkControllerResultInterface
          */
          public function jsonResponse($response = '')

          return $this->getResponse()->representJson(
          $this->jsonHelper->jsonEncode($response)
          );







          share|improve this answer

























          • The problem is that the method „execute” of parent class, which extends 'MagentoFrameworkAppActionAction', is not run. How to resolve this problem? How to do that the action not be redirected to the referrer and the 'execute' method are called during the POST action request?

            – Kamil Szmit
            Jan 9 at 14:15


















          0














          In Magento 2.3, controllers must implement MagentoFrameworkAppActionHttpPostActionInterface to be able to handle POST requests - https://devdocs.magento.com/guides/v2.3/extension-dev-guide/routing.html






          share|improve this answer























          • Adding 'implements MagentoFrameworkAppActionHttpPostActionInterface' to the parent class, which extends 'MagentoFrameworkAppActionAction', and to child class, which corresponds to the controller action, change nothing. What should I change additionally?

            – Kamil Szmit
            Jan 9 at 15:01



















          0














          For future reference,



          I was only able to make my request work by doing the following steps in Magento 2.3.1 otherwise I was getting a 404. I checked class vendor/magento/module-authorizenet/Controller/Directpost/Payment/BackendResponse.php for reference.



          1. Add the following interfaces

           CsrfAwareActionInterface, (especially important because all post requests are using by default CSRF validation)
          HttpGetActionInterface,
          HttpPostActionInterface


          1. Implement the CSRF handling functions

          public function validateForCsrf(RequestInterface $request): ?bool

          return true;


          public function createCsrfValidationException(
          RequestInterface $request
          ): ?InvalidRequestException
          return null;



          1. Execute

          php bin/magento setup:upgrade
          php cache:flush





          share|improve this answer























            Your Answer








            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "479"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f257261%2fhow-to-create-ajax-action-for-post-requests-in-magento-2-3%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            To create AJAX action for POST requests in Magento 2.3, Please see the code below.



            namespace BillonPaymentControllerMultishipping;

            class Initiation extends MagentoFrameworkAppActionAction


            protected $resultPageFactory;
            protected $jsonHelper;

            /**
            * Constructor
            *
            * @param MagentoFrameworkAppActionContext $context
            * @param MagentoFrameworkJsonHelperData $jsonHelper
            */
            public function __construct(
            MagentoFrameworkAppActionContext $context,
            MagentoFrameworkViewResultPageFactory $resultPageFactory,
            MagentoFrameworkJsonHelperData $jsonHelper
            )
            $this->resultPageFactory = $resultPageFactory;
            $this->jsonHelper = $jsonHelper;
            parent::__construct($context);


            /**
            * Execute view action
            *
            * @return MagentoFrameworkControllerResultInterface
            */
            public function execute()

            try
            return $this->jsonResponse('your response');
            catch (MagentoFrameworkExceptionLocalizedException $e)
            return $this->jsonResponse($e->getMessage());
            catch (Exception $e)
            $this->logger->critical($e);
            return $this->jsonResponse($e->getMessage());



            /**
            * Create json response
            *
            * @return MagentoFrameworkControllerResultInterface
            */
            public function jsonResponse($response = '')

            return $this->getResponse()->representJson(
            $this->jsonHelper->jsonEncode($response)
            );







            share|improve this answer

























            • The problem is that the method „execute” of parent class, which extends 'MagentoFrameworkAppActionAction', is not run. How to resolve this problem? How to do that the action not be redirected to the referrer and the 'execute' method are called during the POST action request?

              – Kamil Szmit
              Jan 9 at 14:15















            0














            To create AJAX action for POST requests in Magento 2.3, Please see the code below.



            namespace BillonPaymentControllerMultishipping;

            class Initiation extends MagentoFrameworkAppActionAction


            protected $resultPageFactory;
            protected $jsonHelper;

            /**
            * Constructor
            *
            * @param MagentoFrameworkAppActionContext $context
            * @param MagentoFrameworkJsonHelperData $jsonHelper
            */
            public function __construct(
            MagentoFrameworkAppActionContext $context,
            MagentoFrameworkViewResultPageFactory $resultPageFactory,
            MagentoFrameworkJsonHelperData $jsonHelper
            )
            $this->resultPageFactory = $resultPageFactory;
            $this->jsonHelper = $jsonHelper;
            parent::__construct($context);


            /**
            * Execute view action
            *
            * @return MagentoFrameworkControllerResultInterface
            */
            public function execute()

            try
            return $this->jsonResponse('your response');
            catch (MagentoFrameworkExceptionLocalizedException $e)
            return $this->jsonResponse($e->getMessage());
            catch (Exception $e)
            $this->logger->critical($e);
            return $this->jsonResponse($e->getMessage());



            /**
            * Create json response
            *
            * @return MagentoFrameworkControllerResultInterface
            */
            public function jsonResponse($response = '')

            return $this->getResponse()->representJson(
            $this->jsonHelper->jsonEncode($response)
            );







            share|improve this answer

























            • The problem is that the method „execute” of parent class, which extends 'MagentoFrameworkAppActionAction', is not run. How to resolve this problem? How to do that the action not be redirected to the referrer and the 'execute' method are called during the POST action request?

              – Kamil Szmit
              Jan 9 at 14:15













            0












            0








            0







            To create AJAX action for POST requests in Magento 2.3, Please see the code below.



            namespace BillonPaymentControllerMultishipping;

            class Initiation extends MagentoFrameworkAppActionAction


            protected $resultPageFactory;
            protected $jsonHelper;

            /**
            * Constructor
            *
            * @param MagentoFrameworkAppActionContext $context
            * @param MagentoFrameworkJsonHelperData $jsonHelper
            */
            public function __construct(
            MagentoFrameworkAppActionContext $context,
            MagentoFrameworkViewResultPageFactory $resultPageFactory,
            MagentoFrameworkJsonHelperData $jsonHelper
            )
            $this->resultPageFactory = $resultPageFactory;
            $this->jsonHelper = $jsonHelper;
            parent::__construct($context);


            /**
            * Execute view action
            *
            * @return MagentoFrameworkControllerResultInterface
            */
            public function execute()

            try
            return $this->jsonResponse('your response');
            catch (MagentoFrameworkExceptionLocalizedException $e)
            return $this->jsonResponse($e->getMessage());
            catch (Exception $e)
            $this->logger->critical($e);
            return $this->jsonResponse($e->getMessage());



            /**
            * Create json response
            *
            * @return MagentoFrameworkControllerResultInterface
            */
            public function jsonResponse($response = '')

            return $this->getResponse()->representJson(
            $this->jsonHelper->jsonEncode($response)
            );







            share|improve this answer















            To create AJAX action for POST requests in Magento 2.3, Please see the code below.



            namespace BillonPaymentControllerMultishipping;

            class Initiation extends MagentoFrameworkAppActionAction


            protected $resultPageFactory;
            protected $jsonHelper;

            /**
            * Constructor
            *
            * @param MagentoFrameworkAppActionContext $context
            * @param MagentoFrameworkJsonHelperData $jsonHelper
            */
            public function __construct(
            MagentoFrameworkAppActionContext $context,
            MagentoFrameworkViewResultPageFactory $resultPageFactory,
            MagentoFrameworkJsonHelperData $jsonHelper
            )
            $this->resultPageFactory = $resultPageFactory;
            $this->jsonHelper = $jsonHelper;
            parent::__construct($context);


            /**
            * Execute view action
            *
            * @return MagentoFrameworkControllerResultInterface
            */
            public function execute()

            try
            return $this->jsonResponse('your response');
            catch (MagentoFrameworkExceptionLocalizedException $e)
            return $this->jsonResponse($e->getMessage());
            catch (Exception $e)
            $this->logger->critical($e);
            return $this->jsonResponse($e->getMessage());



            /**
            * Create json response
            *
            * @return MagentoFrameworkControllerResultInterface
            */
            public function jsonResponse($response = '')

            return $this->getResponse()->representJson(
            $this->jsonHelper->jsonEncode($response)
            );








            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 9 at 13:43









            Piyush

            4,81472055




            4,81472055










            answered Jan 9 at 12:48









            Anuj SharmaAnuj Sharma

            162




            162












            • The problem is that the method „execute” of parent class, which extends 'MagentoFrameworkAppActionAction', is not run. How to resolve this problem? How to do that the action not be redirected to the referrer and the 'execute' method are called during the POST action request?

              – Kamil Szmit
              Jan 9 at 14:15

















            • The problem is that the method „execute” of parent class, which extends 'MagentoFrameworkAppActionAction', is not run. How to resolve this problem? How to do that the action not be redirected to the referrer and the 'execute' method are called during the POST action request?

              – Kamil Szmit
              Jan 9 at 14:15
















            The problem is that the method „execute” of parent class, which extends 'MagentoFrameworkAppActionAction', is not run. How to resolve this problem? How to do that the action not be redirected to the referrer and the 'execute' method are called during the POST action request?

            – Kamil Szmit
            Jan 9 at 14:15





            The problem is that the method „execute” of parent class, which extends 'MagentoFrameworkAppActionAction', is not run. How to resolve this problem? How to do that the action not be redirected to the referrer and the 'execute' method are called during the POST action request?

            – Kamil Szmit
            Jan 9 at 14:15













            0














            In Magento 2.3, controllers must implement MagentoFrameworkAppActionHttpPostActionInterface to be able to handle POST requests - https://devdocs.magento.com/guides/v2.3/extension-dev-guide/routing.html






            share|improve this answer























            • Adding 'implements MagentoFrameworkAppActionHttpPostActionInterface' to the parent class, which extends 'MagentoFrameworkAppActionAction', and to child class, which corresponds to the controller action, change nothing. What should I change additionally?

              – Kamil Szmit
              Jan 9 at 15:01
















            0














            In Magento 2.3, controllers must implement MagentoFrameworkAppActionHttpPostActionInterface to be able to handle POST requests - https://devdocs.magento.com/guides/v2.3/extension-dev-guide/routing.html






            share|improve this answer























            • Adding 'implements MagentoFrameworkAppActionHttpPostActionInterface' to the parent class, which extends 'MagentoFrameworkAppActionAction', and to child class, which corresponds to the controller action, change nothing. What should I change additionally?

              – Kamil Szmit
              Jan 9 at 15:01














            0












            0








            0







            In Magento 2.3, controllers must implement MagentoFrameworkAppActionHttpPostActionInterface to be able to handle POST requests - https://devdocs.magento.com/guides/v2.3/extension-dev-guide/routing.html






            share|improve this answer













            In Magento 2.3, controllers must implement MagentoFrameworkAppActionHttpPostActionInterface to be able to handle POST requests - https://devdocs.magento.com/guides/v2.3/extension-dev-guide/routing.html







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 9 at 12:57









            PaulPaul

            1,13211426




            1,13211426












            • Adding 'implements MagentoFrameworkAppActionHttpPostActionInterface' to the parent class, which extends 'MagentoFrameworkAppActionAction', and to child class, which corresponds to the controller action, change nothing. What should I change additionally?

              – Kamil Szmit
              Jan 9 at 15:01


















            • Adding 'implements MagentoFrameworkAppActionHttpPostActionInterface' to the parent class, which extends 'MagentoFrameworkAppActionAction', and to child class, which corresponds to the controller action, change nothing. What should I change additionally?

              – Kamil Szmit
              Jan 9 at 15:01

















            Adding 'implements MagentoFrameworkAppActionHttpPostActionInterface' to the parent class, which extends 'MagentoFrameworkAppActionAction', and to child class, which corresponds to the controller action, change nothing. What should I change additionally?

            – Kamil Szmit
            Jan 9 at 15:01






            Adding 'implements MagentoFrameworkAppActionHttpPostActionInterface' to the parent class, which extends 'MagentoFrameworkAppActionAction', and to child class, which corresponds to the controller action, change nothing. What should I change additionally?

            – Kamil Szmit
            Jan 9 at 15:01












            0














            For future reference,



            I was only able to make my request work by doing the following steps in Magento 2.3.1 otherwise I was getting a 404. I checked class vendor/magento/module-authorizenet/Controller/Directpost/Payment/BackendResponse.php for reference.



            1. Add the following interfaces

             CsrfAwareActionInterface, (especially important because all post requests are using by default CSRF validation)
            HttpGetActionInterface,
            HttpPostActionInterface


            1. Implement the CSRF handling functions

            public function validateForCsrf(RequestInterface $request): ?bool

            return true;


            public function createCsrfValidationException(
            RequestInterface $request
            ): ?InvalidRequestException
            return null;



            1. Execute

            php bin/magento setup:upgrade
            php cache:flush





            share|improve this answer



























              0














              For future reference,



              I was only able to make my request work by doing the following steps in Magento 2.3.1 otherwise I was getting a 404. I checked class vendor/magento/module-authorizenet/Controller/Directpost/Payment/BackendResponse.php for reference.



              1. Add the following interfaces

               CsrfAwareActionInterface, (especially important because all post requests are using by default CSRF validation)
              HttpGetActionInterface,
              HttpPostActionInterface


              1. Implement the CSRF handling functions

              public function validateForCsrf(RequestInterface $request): ?bool

              return true;


              public function createCsrfValidationException(
              RequestInterface $request
              ): ?InvalidRequestException
              return null;



              1. Execute

              php bin/magento setup:upgrade
              php cache:flush





              share|improve this answer

























                0












                0








                0







                For future reference,



                I was only able to make my request work by doing the following steps in Magento 2.3.1 otherwise I was getting a 404. I checked class vendor/magento/module-authorizenet/Controller/Directpost/Payment/BackendResponse.php for reference.



                1. Add the following interfaces

                 CsrfAwareActionInterface, (especially important because all post requests are using by default CSRF validation)
                HttpGetActionInterface,
                HttpPostActionInterface


                1. Implement the CSRF handling functions

                public function validateForCsrf(RequestInterface $request): ?bool

                return true;


                public function createCsrfValidationException(
                RequestInterface $request
                ): ?InvalidRequestException
                return null;



                1. Execute

                php bin/magento setup:upgrade
                php cache:flush





                share|improve this answer













                For future reference,



                I was only able to make my request work by doing the following steps in Magento 2.3.1 otherwise I was getting a 404. I checked class vendor/magento/module-authorizenet/Controller/Directpost/Payment/BackendResponse.php for reference.



                1. Add the following interfaces

                 CsrfAwareActionInterface, (especially important because all post requests are using by default CSRF validation)
                HttpGetActionInterface,
                HttpPostActionInterface


                1. Implement the CSRF handling functions

                public function validateForCsrf(RequestInterface $request): ?bool

                return true;


                public function createCsrfValidationException(
                RequestInterface $request
                ): ?InvalidRequestException
                return null;



                1. Execute

                php bin/magento setup:upgrade
                php cache:flush






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jun 10 at 7:25









                gabtzigabtzi

                237210




                237210



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Magento Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f257261%2fhow-to-create-ajax-action-for-post-requests-in-magento-2-3%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

                    Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

                    Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?