drupal_json

Definition

drupal_json($var = NULL)
drupal/includes/common.inc, line 2782

Description

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.

Parameters

$var (optional) If set, the variable will be converted to JSON and output.

Related topics

Namesort iconDescription
Input validationFunctions to validate user input.

Code

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);
  }
}