Deadpool Posted October 16, 2018 Share Posted October 16, 2018 Hi Guys, I am using Prestashop 1.7 and I created custom admin module controller class. What is the proper way to call a method inside my controller using AJAX request? For example: class AdminFooBarController extends ModuleAdminController { public function ajaxProcessGetBar() { return 'foo'; } } and in the backoffice template I have: $.ajax({ type: 'POST', cache: false, dataType: 'json', url: 'ajax-tab.php', // not sure about this part data: { ajax: true, controller: 'AdminFooBar', action: 'GetBar', token: token }, success: function (data) { // something magical } }); I can't seem to get it work. Thanks 1 Link to comment Share on other sites More sharing options...
Rolige Posted October 17, 2018 Share Posted October 17, 2018 Hello: Your controller code seems to be right. Try this with the JS: $.ajax({ type: 'POST', cache: false, dataType: 'json', url: 'index.php', // fix this data: { ajax: true, controller: 'AdminFooBar', action: 'getBar', // prestashop already set camel case before execute method token: token }, success: function (data) { // something magical } }); Regards 1 Link to comment Share on other sites More sharing options...
Rhobur Posted October 19, 2018 Share Posted October 19, 2018 try this, $.ajax({ type: 'POST', dataType: 'json', url: 'ajax-tab.php', data: { ajax: true, controller: 'AdminFooBar', action: 'youraction', // small letter case var1: your_variable, // if you want to send some var token: $('#your_DOM_identifier').attr('data-token'), // your token previously set in your DOM element }, }) .done(function (data) { } Link to comment Share on other sites More sharing options...
Deadpool Posted October 23, 2018 Author Share Posted October 23, 2018 Thanks for Your responses. Unfortunately both of propose solution didn't work. The payload looks as follows: request url is {my_domain}/admin/index.php form data: ajax=true&controller=AdminFooBar&action=getbar&token={token} Although the response results in 200, no data is return back. Only security template "INVALID SECURITY TOKEN". Any ideas? Link to comment Share on other sites More sharing options...
fanie Posted February 15, 2019 Share Posted February 15, 2019 (edited) How do you return your data from the php function ? Instead of return $result; you can try die(Tools::JsonEncode($result)); Edited February 15, 2019 by fanie (see edit history) Link to comment Share on other sites More sharing options...
theillo Posted May 5, 2019 Share Posted May 5, 2019 Believe it or not, i think the only error was ajax: true instead of ajax: 1. This worked FINALLY for me: var postdata = { ajax: 1, controller: 'AdminFooBar', action: 'getBar', token: token }; $.ajax({ type: 'POST', url: 'index.php', data: postdata, success: function(r){ //magic } }); Link to comment Share on other sites More sharing options...
haunter Posted September 18, 2019 Share Posted September 18, 2019 On 2/15/2019 at 2:22 PM, fanie said: How do you return your data from the php function ? Instead of return $result; you can try die(Tools::JsonEncode($result)); That's really bad solution. In new PrestaShop you should use JsonResponse in your Symfony type controller. use Symfony\Component\HttpFoundation\JsonResponse; return new JsonResponse([ "1" => [ "ID" => "123-265", "ID eshop"=> "System Architect", "Kategorie"=> "$320,800", "Počet položek"=> "2011/04/25", "Kategorie"=> "Edinburgh", "Podkategorie_1"=> "5421", "Podkategorie_2"=> "5421", "Podkategorie_3"=> "5421", "Počet položek eshop"=> "5421" ] ] ); 1 Link to comment Share on other sites More sharing options...
theillo Posted September 21, 2019 Share Posted September 21, 2019 (edited) On 9/18/2019 at 2:13 AM, haunter said: That's really bad solution. Why? Edited September 21, 2019 by theillo (see edit history) Link to comment Share on other sites More sharing options...
theillo Posted October 14, 2019 Share Posted October 14, 2019 I just ran into a massive problem that took me a long time to figure out. Documenting this here in case it'll be helpful for someone in the future: My ajax calls had been running smoothly so far. But suddenly they stopped working. Turns out there are 2 types of ajax methods: public function ajaxProcessGetBar(){ // this will be called in combination with postProcess() // on your ajax call, if you have submitted $_POST['action'] = 'getBar'; // then this will be called. } public function displayAjaxGetbar(){ //this will be called even without the post process, as long as $_POST['ajax'] = 1; // on your ajax call, if you have submitted $_POST['action'] = 'getBar'; // notice that you have to change the function name to all lower case. } So, here's what's important to consider: class AdminFooBarController extends ModuleAdminController { public function postProcess() { return parent::postProcess(); // IF YOU DON'T DO THIS FOR ANY REASON } public function ajaxProcessGetBar() { return 'foo'; //THIS WON'T BE FUNCTIONING PROPERLY } } Also a nifty little function I found while debugging this: abstract class ControllerCore { /** * Dies and echoes output value * * @param string|null $value * @param string|null $controller * @param string|null $method */ protected function ajaxDie($value = null, $controller = null, $method = null) { if ($controller === null) { $controller = get_class($this); } if ($method === null) { $bt = debug_backtrace(); $method = $bt[1]['function']; } Hook::exec('actionBeforeAjaxDie', array('controller' => $controller, 'method' => $method, 'value' => $value)); Hook::exec('actionBeforeAjaxDie'.$controller.$method, array('value' => $value)); die($value); } } So this function/method ajaxDie is located in the Controller (myshop\classes\controller\Controller.php) You should be able to use it in your module controller to echo ajax values and exit the script, since they all inherit each other (AdminFooBarController extends ModuleAdminController extends AdminControllerCore extends Controller) So, what @haunter said... simply using die(); isn't necessary/or a "good" solution, because there's a specific function made for that: ajaxDie(); and it will even execute the hook actionBeforeAjaxDie for you. Hope this helps someone! 🙂 2 Link to comment Share on other sites More sharing options...
Amir92 Posted April 22, 2020 Share Posted April 22, 2020 On 10/23/2018 at 4:17 AM, Deadpool said: Thanks for Your responses. Unfortunately both of propose solution didn't work. The payload looks as follows: request url is {my_domain}/admin/index.php form data: ajax=true&controller=AdminFooBar&action=getbar&token={token} Although the response results in 200, no data is return back. Only security template "INVALID SECURITY TOKEN". Any ideas? This is very old post, but i have the solution, hope this help someone You'r solution is correct, to fix token issue, get token value like this: $token_foo = Tools::getAdminTokenLite('AdminFooBar'); Link to comment Share on other sites More sharing options...
CristianR Posted August 12, 2020 Share Posted August 12, 2020 I added the full details here First you have to add Ajax link in hook or somewhere with // Create a link with the path $adminajax_link = $this->context->link->getAdminLink('YourAdminModule'); //define js value to use in ajax url Media::addJsDef(array( "adminajax_link" => $adminajax_link )); And in your admin Module code looks like <?php class YourAdminModuleController extends ModuleAdminController { public function ajaxProcessYourActionName() { echo json_encode('foo');//something you want to return exit; } } and in your JS would be $.ajax({ type: 'POST', cache: false, dataType: 'json', url: adminajax_link, data: { ajax: true, action: 'youractionname'//lowercase with action name }, success : function (data) { console.log(data); }, error : function (data){ console.log(data); } }); 1 Link to comment Share on other sites More sharing options...
Estian Posted October 21, 2020 Share Posted October 21, 2020 (edited) Good day - I can not for the life of me get an ajax call to work. PrestaShop: 1.7.6.5 Module: CustomModule Ajax URL: https://domain/module/CustomModule/CustomModuleOrderManagerController?action=productStockIssue&id_product=###&id_order=###&ajax=true(Context::getContext()->link->getModuleLink( 'CustomModule', 'CustomModuleOrderManagerController' )) ORhttps://domain/admin/index.php?controller=CustomModuleOrderManagerController&token=######?action=productStockIssue&id_product=###&id_order=###&ajax=true(Context::getContext()->link->getAdminLink( 'CustomModuleOrderManagerController' )) $.ajax( { type : "GET", url : link, data : { ajax : true, action : 'productStockIssue' }, async : true, cache : false, beforeSend: function() { }, complete: function() { }, success: function( data ) { console.log( data ); }, error: function( XMLHttpRequest, textStatus, errorThrown ) { console.log( XMLHttpRequest ); alert( XMLHttpRequest.responseText ); } } ); class CustomModuleOrderManagerController extends ModuleAdminController { public function displayAjaxProductStockIssue() { $this->ajaxDie( Tools::jsonEncode( ['error' => 'This is a test.'] ) ); } public function processProductStockIssue() { $this->ajaxDie( Tools::jsonEncode( ['error' => 'This is a test 2.'] ) ); } public function ajaxProcessProductStockIssue() { $this->ajaxDie( Tools::jsonEncode( ['error' => 'This is a test 3.'] ) ); } } Am I missing something? It just responds with "404 not found" with everything that I've tried. Edited October 21, 2020 by Estian (see edit history) Link to comment Share on other sites More sharing options...
Estian Posted October 21, 2020 Share Posted October 21, 2020 Turns out I had to register a "tab" (the controller name) for the controller in "installTab()" during module installation. Link to comment Share on other sites More sharing options...
Cryonos80 Posted November 6, 2020 Share Posted November 6, 2020 (edited) On 10/21/2020 at 3:43 PM, Estian said: Turns out I had to register a "tab" (the controller name) for the controller in "installTab()" during module installation. It shouldn't be necessary as in Prestashop >1.7.5 you can load a custom admin controller without registering a Tab. But... the doc is pretty confusing, I failed to do it, does anyone know how to register an admin controller and use it in ajax from backoffice? Edited November 6, 2020 by Cryonos80 (see edit history) Link to comment Share on other sites More sharing options...
ClassyDevs Posted November 25, 2020 Share Posted November 25, 2020 Hi There, There is a plenty of way to integrate ajax functionality to an admin module controller. First you need to bind an ajax function on click of that button. You also need to send the module admin controller link to the js file. You can send the file via custom script tag in your helper form. <?php $link = new Link(); $urladmin = $link->getAdminLink( 'YourControllerName' ); ?> <script type="text/javascript"> var priceExpiryDate = "{$urladmin}"; </script> Then you can use ajax to fire function on the server. $.ajax({ type: 'POST', cache: false, dataType: 'json', url: adminajax_link, data: { ajax: true, action: 'yourActionName'//lowercase with action name }, success : function (data) { console.log(data); }, error : function (data){ console.log(data); } }); Then do your php on code on the function e.g. that is your actionname in your admin controller class. public function ajaxProcessYourActionName() { echo json_encode('foo');//something you want to return exit; } Please mind the camel case of the name. Boom It is done. Link to comment Share on other sites More sharing options...
Estian Posted November 25, 2020 Share Posted November 25, 2020 On 11/6/2020 at 6:10 PM, Cryonos80 said: It shouldn't be necessary as in Prestashop >1.7.5 you can load a custom admin controller without registering a Tab. But... the doc is pretty confusing, I failed to do it, does anyone know how to register an admin controller and use it in ajax from backoffice? This has been some time ago but as far as I remember I just could not get it to work... and then I registered it as a tab and as ClassyDevs said, "Boom!" it worked haha. Link to comment Share on other sites More sharing options...
ClassyDevs Posted November 25, 2020 Share Posted November 25, 2020 Did not you register a tab earlier? Without the tab how did you went to the controller page? Link to comment Share on other sites More sharing options...
Estian Posted November 25, 2020 Share Posted November 25, 2020 The module that the ajax request was added to did not have a controller at first. I had to add it in order for the ajax request to find the path. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now