We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Forcing AJAX responses from an extension

Developer Preview

Bjørn Børresen's avatar
Bjørn Børresen
629 posts
9 years ago
Bjørn Børresen's avatar Bjørn Børresen

I’m converting an EE2 addon to EE3. It has functionality to return AJAX responses on errors and success. For this I used to override Output.php (in memory) and send_ajax_response($data) instead of the standard response if ajax was set.

Is there a recommended way of doing this in EE3?

Specifically, what I’d like to do is hook onto Output->show_message() and display the output as JSON if the user is doing an AJAX request.

       
Kevin Cupp's avatar
Kevin Cupp
791 posts
9 years ago
Kevin Cupp's avatar Kevin Cupp

Why did you need to override Output and send_ajax_response? What are you doing differently that we weren’t?

If your CP controller is sending the response, you can just return an array like this:

return array(
    'ajax' => TRUE,
    'body' => array('some' => 'json data')
);

And we’ll automatically treat it as an AJAX response and encode the array you set in the body key as JSON. Does that do what you need?

Why are you looking to hook into show_message? Does this add-on return ALL EE system messages as AJAX or something?

       
Bjørn Børresen's avatar
Bjørn Børresen
629 posts
9 years ago
Bjørn Børresen's avatar Bjørn Børresen
Why are you looking to hook into show_message? Does this add-on return ALL EE system messages as AJAX or something?

Yes, specifically member registration. Any errors / or success message is returned via JSON if the user has specified ajax=’y’ in Simple Registration’s registration form. So SR basically hooks onto the member registration hooks to do its stuff, and then it lets EE do the actual creation of the member.

       
Kevin Cupp's avatar
Kevin Cupp
791 posts
9 years ago
Kevin Cupp's avatar Kevin Cupp

Ah I see, thanks for the explanation. No there’s no new way to make this easier in EE3. We’ve made AJAX responses from controllers easier, but not your exact use case. Sorry!

       
Bjørn Børresen's avatar
Bjørn Børresen
629 posts
9 years ago
Bjørn Børresen's avatar Bjørn Børresen

Ok, I’ll deprecate the AJAX functionality for the 3.1 release.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.