Skip to Navigation

Preventing Admin Footer Output in Drupal AJAX Calls

2009
Jun
16th

Some administration and debugging modules for Drupal attach their statistics to the end of Drupal rendered pages using PHP's register_shutdown_function function facility. This is great to ensure the output is always available, no matter how badly built the theme being used is.

Examples of modules adding their stats to the footer include the superb Devel package which allows outputting statistics on your database queries (or "Gunk" as its called in the source code), and the Memcache Admin UI module which provides information about the memcache bin usage.

The problem arises when you're using Drupal to output AJAX responses which contain plain text. If your Drupal generated response doesn't have a suitable HTTP header defined for it, then both Devel and Memcache tag on their output to your AJAX response and cause the client-side JavaScript to be very confused, or just fail.

The simple solution is to set a HTTP Header in your custom module before the output. Headers which are recognised by Devel and Memcache vary. Devel recognises
'xml', 'javascript', 'json', 'plain', 'image', 'application', 'csv', and 'x-comma-separated-values', whilst Memcache picks up on 'xml', 'javascript', and 'plain'.

Using the Drupal API add the following before you print your output:

drupal_set_header('Content-Type: text/plain; charset=utf-8');

The Devel module also has an extra switch you can set using $GLOBALS['devel_shutdown'] = FALSE to suppress the output, however that doesn't cover the Memcache case.

Comments

Spot on! Saved my headache getting any worse with my custom AJAX callbacks being meddled with by the devel module. Thanks for the explanation just at the right time!

Post new comment

The content of this field is kept private and will not be shown publicly.