2

What's the proper way to return an AJAX response within an ExpressionEngine module?

Good ole' echo "foo"; die(); seems to work but there must be a better way!

I tried the CodeIgniter approach $this->EE->output->set_output(json_encode($array));

Thanks for the help!

2 Answers 2

5

You can use

$this->EE->output->send_ajax_response($array);

for the same purpose.

1
  • This is good for returning JSON data only it seems as I couldn't get it to return just a string via AJAX. Commented Mar 5, 2013 at 4:38
1

If you are trying to return data other than JSON I ended up using the following instead. It's mostly ripped off from the same send_ajax_response function that Bhashkar was kind enough to lead me to. You should probably choose the proper Content-Type (so maybe if it was XML it would be different?).

$this->EE->config->item('send_headers') == 'y';
@header('Content-Type: text/html; charset=UTF-8');
exit($output);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.