| 7 common.inc | drupal_json_output($var = NULL) |
| 8 common.inc | drupal_json_output($var = NULL) |
Returns 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.
Parameters
$var: (optional) If set, the variable will be converted to JSON and output.
File
- includes/
common.inc, line 4902 - Common functions that many Drupal modules will need to reference.
Code
<?php
function drupal_json_output($var = NULL) {
// We are returning JSON, so tell the browser.
drupal_add_http_header('Content-Type', 'application/json');
if (isset($var)) {
echo drupal_json_encode($var);
}
}
?> Login or register to post comments
Comments
Drupal 6
The equivalent function in Drupal 6 is http://api.drupal.org/api/function/drupal_json/6
See http://drupal.org/update/modules/6/7#rename-drupal-to-js for more information.
However, D6 has some bugs
However, D6 has some bugs about header content-type (not
application/jsonbuttext/javascript) and breaks validation JSON if you are using jQuery update to 1.4. So use D6 version with care.