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=trueit say 302 found error in response. - If I use
isAjax=truefor url ajax it sayinvalid form key.... - When I add
form_key:"form key value"indata:{}of ajax it say404 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.
-
clear cache logout and tryJigs Parmar– Jigs Parmar2019年09月06日 06:25:26 +00:00Commented Sep 6, 2019 at 6:25
2 Answers 2
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
-
Hi, See my updated.Hữu Công Hoàng– Hữu Công Hoàng2017年05月23日 10:56:03 +00:00Commented May 23, 2017 at 10:56
-
Thank you but it still same problem, 404 Forbidden.Hữu Công Hoàng– Hữu Công Hoàng2017年05月23日 12:52:07 +00:00Commented May 23, 2017 at 12:52
-
Can you post your full url?Khoa Truong– Khoa Truong2017年05月23日 12:53:02 +00:00Commented 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/…Hữu Công Hoàng– Hữu Công Hoàng2017年05月23日 13:18:01 +00:00Commented 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.Khoa Truong– Khoa Truong2017年05月23日 13:23:28 +00:00Commented May 23, 2017 at 13:23
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.
Explore related questions
See similar questions with these tags.