4

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

My controller:

class CustomAjax extends \Magento\Backend\App\Action{
protected $resultJsonFactory;
public function __construct(
 \Magento\Backend\App\Action\Context $context,
 \Magento\Framework\Controller\Result\JsonFactory $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.

asked May 23, 2017 at 9:09
1
  • clear cache logout and try Commented Sep 6, 2019 at 6:25

2 Answers 2

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

answered May 23, 2017 at 10:01
8
  • Hi, See my updated. Commented May 23, 2017 at 10:56
  • Thank you but it still same problem, 404 Forbidden. Commented May 23, 2017 at 12:52
  • Can you post your full url? Commented May 23, 2017 at 12:53
  • localhost/magento/admin/mymodule/customAjax/index/key/… And I also tried this: localhost/magento/admin/mymodule/customAjax/index/key/… Commented May 23, 2017 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. Commented May 23, 2017 at 13:23
0

In my case it was due to i had enabled seo urls on after turning it off solves the issue, as my nginx settings was giving response only if the file is .php .html

On the Admin sidebar, go to Stores > Settings > Configuration.

In the left panel where General is expanded, choose Web.

Expand Expansion selector the Search Engine Optimization section. set the value of use Web Server Rewritest to NO.

answered Mar 23 at 8:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.