I am trying to build a simple AJaX menu: There is already a DIV with id ajaxDiv in my HTML structure. Its content should be replaced with the links of menu menu-test. As soon as one menu-link is clicked, the content of the DIV should be replaced with the content of a node.

The problem is that a node’s content is not delivered via AJaX. The function global_ajax_link_response($node = NULL, $type = 'ajax') does return a normal page instead of an AJaX response. Because the nojs token is not removed from the menu-link when clicked. And I don’t know why. Could you please give me some help?

Here is the code of module my_ajax:

function my_ajax_menu() {

$items = array();

// Register global menu callback for URLs beginnig with ajax.
$items['ajax/%'] = array(
'page callback' => 'global_ajax_link_response',
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);

return $items;
}

function ajax_menu() {
$build = array();

$links = menu_navigation_links('menu-test', 0);

foreach($links as $link) {
$build['ajax_link_' . str_replace (' ', '_', $link['title'])] = array(
'#type' => 'link',
'#title' => $link['title'],
'#href' => 'ajax/' . $link['href'] . '/nojs/', // add global identifier 'ajax/' to URL
'#id' => 'ajax-link-' . str_replace (' ', '-', $link['title']),
'#ajax' => array(
'wrapper' => 'ajaxDiv',
'method' => 'html',
),
);
}

$output = drupal_render($build);

$commands = array();
$commands[] = ajax_command_replace('#ajaxDiv', '<div class="left-margin" id="ajaxDiv">' . $output . '</div>');
$page = array('#type' => 'ajax', '#commands' => $commands);
ajax_deliver($page);
}

function global_ajax_link_response($node = NULL, $type = 'ajax') {

// Retrieve themed content of node:
$output = views_embed_view('node_text_content', 'default', $node);

// If not an AJaX request, deliver output via normal page load:
if ($type != 'ajax') {
return $output;
}

// Deliver output via AJaX:
$commands = array();
$commands[] = ajax_command_replace('#ajaxDiv', '<div class="left-margin" id="ajaxDiv">' . $output . '</div>');
$page = array('#type' => 'ajax', '#commands' => $commands);
ajax_deliver($page);
}