rick Posted May 21, 2018 Share Posted May 21, 2018 Howdy all, I am having difficulty getting an AJAX URL to target the appropriate executeSomething method. No matter how I define the URL only the execute() method is called. I am creating a process module based on ProcessHello. I have sub-menu options defined which have their own paths as stated in the module.info.php file. However, an example ajax url defined as "./some-thing" does not access the executeSomeThing method. It only hits the execute() method. I have tried an ajax url as "./sub-menu option path/some-thing" and still no joy. Obviously I am missing something very simple. Can anyone shed some light on this please? Link to comment Share on other sites More sharing options...
Robin S Posted May 21, 2018 Share Posted May 21, 2018 Hi rick, I think you need a trailing slash for AJAX URLs in PW. Try "./some-thing/" 1 Link to comment Share on other sites More sharing options...
rick Posted May 21, 2018 Author Share Posted May 21, 2018 Thanks Robin, but now the complete URL is shown in the network tab, where as only the url specified in the ajax url is shown when omitting the trailing slash. The response data is still the data generated by the execute() function. Originally, I had manually built the menu structure under Admin, and assigned separate process modules to each. The ajax requests worked. Now I am trying to refactor this to removed duplicate code, installing half a dozen process modules, etc. Using the ProcessHello structure, those "physical paths" that ajax was previously using no longer exist. Does this mean that I have to write my own routing procedure in the execute() method? Link to comment Share on other sites More sharing options...
bernhard Posted May 21, 2018 Share Posted May 21, 2018 do you maybe have the page path history module installed? if you had those ajax endpoints as real pages maybe there is some redirection going on... just try your setup on a fresh pw installation. working with process modules and executeSomething is usually really easy ? Link to comment Share on other sites More sharing options...
rick Posted May 21, 2018 Author Share Posted May 21, 2018 Hi bernhard, This is a fresh PW (98) install with only Tracy, Upgrades, and Export Profile modules installed. Link to comment Share on other sites More sharing options...
bernhard Posted May 21, 2018 Share Posted May 21, 2018 15 minutes ago, rick said: This is a fresh PW (98) install with only Tracy, Upgrades, and Export Profile modules installed. 8 hours ago, rick said: No matter how I define the URL only the execute() method is called. maybe you want to share the code how you define your urls and also the related execute method? Link to comment Share on other sites More sharing options...
rick Posted May 21, 2018 Author Share Posted May 21, 2018 My process module is using the ProcessHello files: This is the MyProcess.module.php without all the comments: class MyProcess extends Process { public function init() { $this->config->styles->append ... datatables.css; $this->config->scripts->append ... datatables.js; parent::init(); } // Main menu end point. public function execute() { // Display placehoder content from main menu } // Sub menu end point. // This is the method executed regardless of ajax url setting - see myprocess.js public function executeOption1() { $mytable = <<<_OPT ... _OPT; // myTableID return $mytable; // This is the data shown as the ajax response. } // Ajax end point. // This is the method that should be executed. public function executeOption1List() { // extract data from pages $result[] = [ col1, col2, ...] echo json_encode( $result ); } } MyProcess.info.php $info = array( // title, etc. // page that you want created to execute this module 'page' => array( 'name' => 'myprocess', 'parent' => 'admin', 'title' => 'My Process' ), // optional extra navigation that appears in admin 'nav' => array( array( 'url' => 'option1/', // number only for clarity 'label' => 'Option1', ), array( 'url' => 'option2/', 'label' => 'Option2', ), ) // for more options that you may specify here, see the file: /wire/core/Process.php ); MyProcess.config.php is the example provided in ProcessHello. I'm not using it at the moment. The MyProcess.js: $(document).ready(function() { $('#myTableID').DataTable( { dom: '<lf<t>ip>', processing: true, ajax: { url: './option1-list/', // I've tried with/without trailing slash, with/without './', etc. dataSrc: '' }, columns: [ { blah blah }, ], }); }); Like I said, there is something simple that I am missing, but I can't seem to find it. This works in the stand-alone modules, but does not work in the ProcessHello mode. 1 Link to comment Share on other sites More sharing options...
OLSA Posted May 21, 2018 Share Posted May 21, 2018 Can you try to change url (without "-" separator) ajax: { url: './option1list/', dataSrc: '' }, 1 Link to comment Share on other sites More sharing options...
rick Posted May 21, 2018 Author Share Posted May 21, 2018 59 minutes ago, OLSA said: Can you try to change url (without "-" separator) Yes, I tried that, along with many other permutations. Now, I'm in the middle of starting over again with a fresh install. Link to comment Share on other sites More sharing options...
Robin S Posted May 21, 2018 Share Posted May 21, 2018 When you use a single "." at the start of a URL it means "the current path". Your Javascript executes from "/admin/myprocess/option1/", so when you define the AJAX URL as "./option1-list/" the resulting URL will be "/admin/myprocess/option1/option1-list/". There is no execute method for that URL. Generally you would expect a 404 error, but because URL segments are enabled for Process modules the URL segments are parsed and the valid segment "option1/" is found, and the "option1-list/" doesn't resolve to anything so is effectively ignored. So that is why executeOption1() is executing and you are getting that data back in the AJAX response. For your own clarity I suggest assigning an absolute URL to a variable in Javascript and using that as your AJAX URL. That way you can log the URL to debug and be sure it is what you are expecting. var url = ProcessWire.config.urls.admin + 'myprocess/option1-list/'; console.log("url: " + url); // ... 4 Link to comment Share on other sites More sharing options...
rick Posted May 21, 2018 Author Share Posted May 21, 2018 Bingo! We have a winner! Everyone can go home now, @Robin S done won the prize. ? I cannot tell you how long that would have taken me to figure out. I'll give you a hint: I'm old and don't have that much time. Thank you and thank you to everyone that helped. This forum community makes a defining difference between other CMSs. 2 Link to comment Share on other sites More sharing options...
bernhard Posted May 22, 2018 Share Posted May 22, 2018 17 hours ago, rick said: url: './option1-list/', // I've tried with/without trailing slash, with/without './', etc. So this comment was wrong ? ? Link to comment Share on other sites More sharing options...
rick Posted May 22, 2018 Author Share Posted May 22, 2018 Yeah. ? I didn't try the full path as Robin pointed out. 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