Ajax not working in Magento 2 admin












3














I'm using ajax in Magento 2 admin. But it not working.



My controller:



class CustomAjax extends MagentoBackendAppAction{

protected $resultJsonFactory;

public function __construct(
MagentoBackendAppActionContext $context,
MagentoFrameworkControllerResultJsonFactory $resultJsonFactory

)
{
parent::__construct($context);
$this->resultFactory = $resultJsonFactory;
}

public function execute()
{
$resultJson = $this->resultJsonFactory->create();

return $resultJson->setData([
'messages' => 'Successfully. Params: ',
'error' => false
]);
}
public function indexAction(){
return $this->resultJsonFactory->create()->setData(['a' => 'b']);
}}



  • If I use url of jQuery ajax without isAjax=true it say 302 found error in response.

  • If I use isAjax=true for url ajax it say invalid form key....

  • When I add form_key: "form key value" in data:{} of ajax it say 404 Forbidden.


Please help me, thank so much!



--- update



My url in this case is
http://localhost/magento/admin/mymodule/customAjax/index/key/417a3857c3ee5d1ec2c2c474ce5ce0a217779619d313b12e0d668b950ac80d9d/?isAjax=true



and this is ajax:



jQuery.ajax({
type:"POST",
url: ajaxUrl,
data:{
form_key: formKey
},
beforeSend : function(data){

},
success : function(data){

}
});


formKey in this case is u5XSzd7EmRKQM7kN



But it say 404 Forbidden error.










share|improve this question
















bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.




















    3














    I'm using ajax in Magento 2 admin. But it not working.



    My controller:



    class CustomAjax extends MagentoBackendAppAction{

    protected $resultJsonFactory;

    public function __construct(
    MagentoBackendAppActionContext $context,
    MagentoFrameworkControllerResultJsonFactory $resultJsonFactory

    )
    {
    parent::__construct($context);
    $this->resultFactory = $resultJsonFactory;
    }

    public function execute()
    {
    $resultJson = $this->resultJsonFactory->create();

    return $resultJson->setData([
    'messages' => 'Successfully. Params: ',
    'error' => false
    ]);
    }
    public function indexAction(){
    return $this->resultJsonFactory->create()->setData(['a' => 'b']);
    }}



    • If I use url of jQuery ajax without isAjax=true it say 302 found error in response.

    • If I use isAjax=true for url ajax it say invalid form key....

    • When I add form_key: "form key value" in data:{} of ajax it say 404 Forbidden.


    Please help me, thank so much!



    --- update



    My url in this case is
    http://localhost/magento/admin/mymodule/customAjax/index/key/417a3857c3ee5d1ec2c2c474ce5ce0a217779619d313b12e0d668b950ac80d9d/?isAjax=true



    and this is ajax:



    jQuery.ajax({
    type:"POST",
    url: ajaxUrl,
    data:{
    form_key: formKey
    },
    beforeSend : function(data){

    },
    success : function(data){

    }
    });


    formKey in this case is u5XSzd7EmRKQM7kN



    But it say 404 Forbidden error.










    share|improve this question
















    bumped to the homepage by Community yesterday


    This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.


















      3












      3








      3







      I'm using ajax in Magento 2 admin. But it not working.



      My controller:



      class CustomAjax extends MagentoBackendAppAction{

      protected $resultJsonFactory;

      public function __construct(
      MagentoBackendAppActionContext $context,
      MagentoFrameworkControllerResultJsonFactory $resultJsonFactory

      )
      {
      parent::__construct($context);
      $this->resultFactory = $resultJsonFactory;
      }

      public function execute()
      {
      $resultJson = $this->resultJsonFactory->create();

      return $resultJson->setData([
      'messages' => 'Successfully. Params: ',
      'error' => false
      ]);
      }
      public function indexAction(){
      return $this->resultJsonFactory->create()->setData(['a' => 'b']);
      }}



      • If I use url of jQuery ajax without isAjax=true it say 302 found error in response.

      • If I use isAjax=true for url ajax it say invalid form key....

      • When I add form_key: "form key value" in data:{} of ajax it say 404 Forbidden.


      Please help me, thank so much!



      --- update



      My url in this case is
      http://localhost/magento/admin/mymodule/customAjax/index/key/417a3857c3ee5d1ec2c2c474ce5ce0a217779619d313b12e0d668b950ac80d9d/?isAjax=true



      and this is ajax:



      jQuery.ajax({
      type:"POST",
      url: ajaxUrl,
      data:{
      form_key: formKey
      },
      beforeSend : function(data){

      },
      success : function(data){

      }
      });


      formKey in this case is u5XSzd7EmRKQM7kN



      But it say 404 Forbidden error.










      share|improve this question















      I'm using ajax in Magento 2 admin. But it not working.



      My controller:



      class CustomAjax extends MagentoBackendAppAction{

      protected $resultJsonFactory;

      public function __construct(
      MagentoBackendAppActionContext $context,
      MagentoFrameworkControllerResultJsonFactory $resultJsonFactory

      )
      {
      parent::__construct($context);
      $this->resultFactory = $resultJsonFactory;
      }

      public function execute()
      {
      $resultJson = $this->resultJsonFactory->create();

      return $resultJson->setData([
      'messages' => 'Successfully. Params: ',
      'error' => false
      ]);
      }
      public function indexAction(){
      return $this->resultJsonFactory->create()->setData(['a' => 'b']);
      }}



      • If I use url of jQuery ajax without isAjax=true it say 302 found error in response.

      • If I use isAjax=true for url ajax it say invalid form key....

      • When I add form_key: "form key value" in data:{} of ajax it say 404 Forbidden.


      Please help me, thank so much!



      --- update



      My url in this case is
      http://localhost/magento/admin/mymodule/customAjax/index/key/417a3857c3ee5d1ec2c2c474ce5ce0a217779619d313b12e0d668b950ac80d9d/?isAjax=true



      and this is ajax:



      jQuery.ajax({
      type:"POST",
      url: ajaxUrl,
      data:{
      form_key: formKey
      },
      beforeSend : function(data){

      },
      success : function(data){

      }
      });


      formKey in this case is u5XSzd7EmRKQM7kN



      But it say 404 Forbidden error.







      magento2 adminhtml ajax admin-controller






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 23 '17 at 10:54

























      asked May 23 '17 at 9:09









      Hữu Công Hoàng

      215




      215





      bumped to the homepage by Community yesterday


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







      bumped to the homepage by Community yesterday


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Seem that your Ajax is missing the form key param. We need to add the form key value to url. For example:



          var form = jQuery('#edit_form');
          var action = form.attr('action');

          //Assume your form action form.attr('action'): http://{admin_url}/custom/ajax/key/f7f0ad18f378d5458e936d216981f1418702a8381da453a094b6273410f2eb0e/

          var newActionUrl = action + '?' + encodeURIComponent(form.find('[name="form_key"]').val());


          You can see an example: vendor/magento/module-import-export/view/adminhtml/templates/import/form/before.phtml






          share|improve this answer





















          • Hi, See my updated.
            – Hữu Công Hoàng
            May 23 '17 at 10:56










          • Thank you but it still same problem, 404 Forbidden.
            – Hữu Công Hoàng
            May 23 '17 at 12:52










          • Can you post your full url?
            – Khoa TruongDinh
            May 23 '17 at 12:53










          • localhost/magento/admin/mymodule/customAjax/index/key/… And I also tried this: localhost/magento/admin/mymodule/customAjax/index/key/…
            – Hữu Công Hoàng
            May 23 '17 at 13:18












          • First of all, I think you should check the custom admin url work or not. For example, try to var_dump or die. Make sure the custom admin url works. In this case, don't need to use form key. Second, try with the ajax and form key.
            – Khoa TruongDinh
            May 23 '17 at 13:23













          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%2f175666%2fajax-not-working-in-magento-2-admin%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Seem that your Ajax is missing the form key param. We need to add the form key value to url. For example:



          var form = jQuery('#edit_form');
          var action = form.attr('action');

          //Assume your form action form.attr('action'): http://{admin_url}/custom/ajax/key/f7f0ad18f378d5458e936d216981f1418702a8381da453a094b6273410f2eb0e/

          var newActionUrl = action + '?' + encodeURIComponent(form.find('[name="form_key"]').val());


          You can see an example: vendor/magento/module-import-export/view/adminhtml/templates/import/form/before.phtml






          share|improve this answer





















          • Hi, See my updated.
            – Hữu Công Hoàng
            May 23 '17 at 10:56










          • Thank you but it still same problem, 404 Forbidden.
            – Hữu Công Hoàng
            May 23 '17 at 12:52










          • Can you post your full url?
            – Khoa TruongDinh
            May 23 '17 at 12:53










          • localhost/magento/admin/mymodule/customAjax/index/key/… And I also tried this: localhost/magento/admin/mymodule/customAjax/index/key/…
            – Hữu Công Hoàng
            May 23 '17 at 13:18












          • First of all, I think you should check the custom admin url work or not. For example, try to var_dump or die. Make sure the custom admin url works. In this case, don't need to use form key. Second, try with the ajax and form key.
            – Khoa TruongDinh
            May 23 '17 at 13:23


















          0














          Seem that your Ajax is missing the form key param. We need to add the form key value to url. For example:



          var form = jQuery('#edit_form');
          var action = form.attr('action');

          //Assume your form action form.attr('action'): http://{admin_url}/custom/ajax/key/f7f0ad18f378d5458e936d216981f1418702a8381da453a094b6273410f2eb0e/

          var newActionUrl = action + '?' + encodeURIComponent(form.find('[name="form_key"]').val());


          You can see an example: vendor/magento/module-import-export/view/adminhtml/templates/import/form/before.phtml






          share|improve this answer





















          • Hi, See my updated.
            – Hữu Công Hoàng
            May 23 '17 at 10:56










          • Thank you but it still same problem, 404 Forbidden.
            – Hữu Công Hoàng
            May 23 '17 at 12:52










          • Can you post your full url?
            – Khoa TruongDinh
            May 23 '17 at 12:53










          • localhost/magento/admin/mymodule/customAjax/index/key/… And I also tried this: localhost/magento/admin/mymodule/customAjax/index/key/…
            – Hữu Công Hoàng
            May 23 '17 at 13:18












          • First of all, I think you should check the custom admin url work or not. For example, try to var_dump or die. Make sure the custom admin url works. In this case, don't need to use form key. Second, try with the ajax and form key.
            – Khoa TruongDinh
            May 23 '17 at 13:23
















          0












          0








          0






          Seem that your Ajax is missing the form key param. We need to add the form key value to url. For example:



          var form = jQuery('#edit_form');
          var action = form.attr('action');

          //Assume your form action form.attr('action'): http://{admin_url}/custom/ajax/key/f7f0ad18f378d5458e936d216981f1418702a8381da453a094b6273410f2eb0e/

          var newActionUrl = action + '?' + encodeURIComponent(form.find('[name="form_key"]').val());


          You can see an example: vendor/magento/module-import-export/view/adminhtml/templates/import/form/before.phtml






          share|improve this answer












          Seem that your Ajax is missing the form key param. We need to add the form key value to url. For example:



          var form = jQuery('#edit_form');
          var action = form.attr('action');

          //Assume your form action form.attr('action'): http://{admin_url}/custom/ajax/key/f7f0ad18f378d5458e936d216981f1418702a8381da453a094b6273410f2eb0e/

          var newActionUrl = action + '?' + encodeURIComponent(form.find('[name="form_key"]').val());


          You can see an example: vendor/magento/module-import-export/view/adminhtml/templates/import/form/before.phtml







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered May 23 '17 at 10:01









          Khoa TruongDinh

          20.9k63884




          20.9k63884












          • Hi, See my updated.
            – Hữu Công Hoàng
            May 23 '17 at 10:56










          • Thank you but it still same problem, 404 Forbidden.
            – Hữu Công Hoàng
            May 23 '17 at 12:52










          • Can you post your full url?
            – Khoa TruongDinh
            May 23 '17 at 12:53










          • localhost/magento/admin/mymodule/customAjax/index/key/… And I also tried this: localhost/magento/admin/mymodule/customAjax/index/key/…
            – Hữu Công Hoàng
            May 23 '17 at 13:18












          • First of all, I think you should check the custom admin url work or not. For example, try to var_dump or die. Make sure the custom admin url works. In this case, don't need to use form key. Second, try with the ajax and form key.
            – Khoa TruongDinh
            May 23 '17 at 13:23




















          • Hi, See my updated.
            – Hữu Công Hoàng
            May 23 '17 at 10:56










          • Thank you but it still same problem, 404 Forbidden.
            – Hữu Công Hoàng
            May 23 '17 at 12:52










          • Can you post your full url?
            – Khoa TruongDinh
            May 23 '17 at 12:53










          • localhost/magento/admin/mymodule/customAjax/index/key/… And I also tried this: localhost/magento/admin/mymodule/customAjax/index/key/…
            – Hữu Công Hoàng
            May 23 '17 at 13:18












          • First of all, I think you should check the custom admin url work or not. For example, try to var_dump or die. Make sure the custom admin url works. In this case, don't need to use form key. Second, try with the ajax and form key.
            – Khoa TruongDinh
            May 23 '17 at 13:23


















          Hi, See my updated.
          – Hữu Công Hoàng
          May 23 '17 at 10:56




          Hi, See my updated.
          – Hữu Công Hoàng
          May 23 '17 at 10:56












          Thank you but it still same problem, 404 Forbidden.
          – Hữu Công Hoàng
          May 23 '17 at 12:52




          Thank you but it still same problem, 404 Forbidden.
          – Hữu Công Hoàng
          May 23 '17 at 12:52












          Can you post your full url?
          – Khoa TruongDinh
          May 23 '17 at 12:53




          Can you post your full url?
          – Khoa TruongDinh
          May 23 '17 at 12:53












          localhost/magento/admin/mymodule/customAjax/index/key/… And I also tried this: localhost/magento/admin/mymodule/customAjax/index/key/…
          – Hữu Công Hoàng
          May 23 '17 at 13:18






          localhost/magento/admin/mymodule/customAjax/index/key/… And I also tried this: localhost/magento/admin/mymodule/customAjax/index/key/…
          – Hữu Công Hoàng
          May 23 '17 at 13:18














          First of all, I think you should check the custom admin url work or not. For example, try to var_dump or die. Make sure the custom admin url works. In this case, don't need to use form key. Second, try with the ajax and form key.
          – Khoa TruongDinh
          May 23 '17 at 13:23






          First of all, I think you should check the custom admin url work or not. For example, try to var_dump or die. Make sure the custom admin url works. In this case, don't need to use form key. Second, try with the ajax and form key.
          – Khoa TruongDinh
          May 23 '17 at 13:23




















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f175666%2fajax-not-working-in-magento-2-admin%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

          1300-talet

          1300-talet

          Has there ever been an instance of an active nuclear power plant within or near a war zone?