Creating a JSON callback page in a Drupal module
This can useful for AJAX scripts or REST applications that need JSON output. In your my_module.module
, extend your hook_menu()
:
/**
* Implements hook_menu().
*/
function my_module_menu() {
$items = array();
// ...
// issue #1210: provide a passthrough for blog post comments
$items['my/callback] = array(
'title' => 'My callback',
'description' => 'My callback description',
'access arguments' => TRUE,
'access callback' => TRUE,
'page callback' => 'my_module_json_callback',
'file' => 'my_module.inc',
);
return $items;
}
And then in my_module.inc
, use drupal_json_output
:
/**
* Page callback.
*/
function my_module_json_callback() {
$json = array(
'hello' => 'world',
'meow' => array(1,2,3),
);
drupal_json_output($json);
}
You can then access your callback (after clearing your caches) by calling http://localhost/drupal/my/callback.