1

My ajax call not working . It returns

"A technical problem with the server created an error. Try again to continue what you were doing. If the problem persists, try again later."

my ajax call code

function validateAuthorize()
{
    require([
        "jquery"
    ]

, function($){

var name = $('#name').val();
var email = $('#email').val();
$.ajax({
    url: '<?php echo $block->getUrl('path/ajaxcontroller', ['_nosid' => true]); ?>',
    showLoader: true,
    data: {name:name,email:email,form_key:'<?php echo $block->getFormKey(); ?>'},
    type: "POST",
    success: function(result){
        jQuery('#message').html(result);
    }
});

}

controller

My\Module\Controller\Adminhtml\Ajaxcontroller\Index.php

<?php
    namespace My\Module\Controller\Adminhtml\Ajaxcontroller;

    class Index extends \Magento\Backend\App\Action
    {
        protected $resultPageFactory;

        public function __construct(
            \Magento\Backend\App\Action\Context $context,
            \Magento\Framework\Controller\Result\JsonFactory $resultPageFactory
        ) {
            parent::__construct($context);
            $this->resultPageFactory = $resultPageFactory;
        }

        public function execute()
        {
            if($this->getRequest()->isAjax()){
                echo "Testing";
            }
            return false;
        }
    }

Earlier ajax was working properly currently it returns

A technical problem with the server created an error. Try again to continue what you were doing. If the problem persists, try again later

12
  • I already answered it. magento.stackexchange.com/a/250735/59216 Commented Dec 11, 2018 at 10:51
  • 1
    Possible duplicate of How to make ajax call in magento admin form Commented Dec 11, 2018 at 10:52
  • @MilindSingh , earlier my ajax code working , now it returns error 404 and display the message " A technical problem with the server created an error. Try again to continue what you were doing. If the problem persists, try again later " Commented Dec 12, 2018 at 6:39
  • you should try posting form_key as a post param not in URL.
    – Zahirabbas
    Commented Dec 12, 2018 at 6:42
  • Have you updated the code as in the answer? Commented Dec 12, 2018 at 6:43

1 Answer 1

2

Change ajax code like below way :

$.ajax({
        url: 'admin/my_module/ajaxcontroller/index',
        showLoader: true,
        data: {name:name,email:email,form_key:'<?php echo $block->getFormKey(); ?>'},
        type: "POST",
        success: function(result){
            jQuery('#message').html(result);
        }
    });
}
7
  • 1
    this is not working .I call ajax in dashboard page . in ajax call while i inspect it call below url"localhost/magento/adminpage/path/dashboard/index/key/… " Commented Dec 12, 2018 at 7:31
  • This is not call that controller ajaxcontroller/index controller Commented Dec 12, 2018 at 7:32
  • just use this code for base admin url : <?php echo $this->helper('Magento\Backend\Helper\Data')->getHomePageUrl(); ?> Commented Dec 12, 2018 at 8:34
  • Then combine => my_module/ajaxcontroller/index Commented Dec 12, 2018 at 8:35
  • 1
    Happy to help !! Happy coding :) Commented Dec 12, 2018 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.