From 569ede574f2db14d7359b42e1ccbf5772a4554fb Mon Sep 17 00:00:00 2001 From: Steven Wittens Date: Fri, 22 Jun 2007 08:46:16 +0000 Subject: #115139: Add centralized json handler (and send text/javascript content-type header). --- includes/common.inc | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'includes') diff --git a/includes/common.inc b/includes/common.inc index 9b4264f49..c371d40d8 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -2122,6 +2122,24 @@ function drupal_to_js($var) { } } +/** + * Return data in JSON format. + * + * This function should be used for JavaScript callback functions returning + * data in JSON format. It sets the header for JavaScript output. + * + * @param $var + * (optional) If set, the variable will be converted to JSON and output. + */ +function drupal_json($var = NULL) { + // We are returning JavaScript, so tell the browser. + drupal_set_header('Content-Type: text/javascript; charset=utf-8'); + + if (isset($var)) { + echo drupal_to_js($var); + } +} + /** * Wrapper around urlencode() which avoids Apache quirks. * -- cgit v1.2.3