Jump to content

Output pure JSON for ajax calls in process module


gebeer
 Share

Recommended Posts

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

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

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

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

  • 2 years later...
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 ?

  • Thanks 1
Link to comment
Share on other sites

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...