gebeer Posted April 15, 2016 Share Posted April 15, 2016 Hello all, I'm pretty new to Process module development and couldn't find much related to my scenario. In my custom process module I have an execute function that returns data for AJAX requests. I would like to output pure JSON. Thus I need to alter the header to be 'Content-Type: application/json' and also not render all the admin theme output. How would I achieve that from within a process module? Or do I need to take care of that on the admin theme level? I have a custom admin theme running. Thank you. Link to comment Share on other sites More sharing options...
gebeer Posted April 15, 2016 Author Share Posted April 15, 2016 It turns out that there is no need for special setup except for setting the right header type. My execute function looks like this and returns pure JSON: public function ___executeAjaxControl() { // handle banword saving if($this->input->get->banword) { $banword = wire('sanitizer')->text($this->input->get->banword); if(wire('pages')->get("template=banword,title={$banword}")->id) { $out = "Stoppwort existiert schon"; } else { $b = new Page(); $b->template = 'banword'; $b->parent = wire('pages')->get('template=banwords'); $b->title = $banword; $b->name = wire('sanitizer')->pageName($banword); try { $b->save(); } catch (Exception $e) { $out = "Es trat ein Fehler auf:{$e->getMessage()}.Bitte erneut speichern."; } if($b->id) $out = "Stoppwort {$b->title} wurde gespeichert"; } } header('Content-Type: application/json'); return json_encode($out); } @admin: I can't mark this as solved 1 Link to comment Share on other sites More sharing options...
gebeer Posted April 23, 2016 Author Share Posted April 23, 2016 The code in #2 works fine on a 3.0.12 install. But the same code in a 2.7.2 install returns not only json_encode($out) but the whole page markup. So I am back at where I was when I originally posted this. How can I get back only what I return from my function in #2 without all the markup of the admin theme? Forget about it - Its getting late here. All working fine. Link to comment Share on other sites More sharing options...
Martijn Geerts Posted April 23, 2016 Share Posted April 23, 2016 echo json_encode($out); return $this->halt(); 1 Link to comment Share on other sites More sharing options...
gebeer Posted April 23, 2016 Author Share Posted April 23, 2016 Thanks, Martijn, for looking into this. It was a really stupid mistake on my side that caused the problem. Code in #2 is working fine. 1 Link to comment Share on other sites More sharing options...
Martijn Geerts Posted April 24, 2016 Share Posted April 24, 2016 We all do stupid things. Every time again... Link to comment Share on other sites More sharing options...
pwFoo Posted June 17, 2018 Share Posted June 17, 2018 On 4/15/2016 at 10:36 AM, gebeer said: It turns out that there is no need for special setup except for setting the right header type. My execute function looks like this and returns pure JSON: Changed that behavior? I get all the admin theme / page stuff... public function execute() { $array = array('me' => 'you'); header('Content-Type: application/json'); echo json_encode($array); exit(); } exit is needed to get plain json array as expected ? 1 Link to comment Share on other sites More sharing options...
Macrura Posted June 17, 2018 Share Posted June 17, 2018 9 hours ago, pwFoo said: exit is needed to get plain json array as expected depends on if you are appending a _main.php file; you can also disable that behavior on the template settings; or do it using variables in your templates; As far as I'm aware, generally return $this->halt() is now recommended over exit() but may not work in 100% of scenarios so exit can be used as a contingency... Link to comment Share on other sites More sharing options...
pwFoo Posted June 17, 2018 Share Posted June 17, 2018 It looks like halt() doesn't work inside of modules? Link to comment Share on other sites More sharing options...
Soma Posted June 18, 2018 Share Posted June 18, 2018 It's $this->halt() only for in templates. In modules you don't need to. 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