Ajax not working in Magento 2 admin
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 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.
magento2 adminhtml ajax admin-controller
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.
add a comment |
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 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.
magento2 adminhtml ajax admin-controller
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.
add a comment |
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 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.
magento2 adminhtml ajax admin-controller
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 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.
magento2 adminhtml ajax admin-controller
magento2 adminhtml ajax admin-controller
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.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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
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
|
show 4 more comments
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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
|
show 4 more comments
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
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
|
show 4 more comments
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
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 '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
|
show 4 more comments
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
|
show 4 more comments
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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