3

I am trying to load a view with Ajax, showing it in a specific <div>. I always get the Ajax message, "You are not authorized to access this page." My code is the following one:

/**
* Implementation of hook_menu().
*/
function custom_api_menu() {
  $items['ajaxgettip/nojs/%'] = array(
    'page callback' => 'custom_api_test_ajax',
    'type' => MENU_CALLBACK,
    'access callback'   => TRUE,

      );
  $items['ajaxgettip/ajax/%'] = array(
     'delivery callback' => 'ajax_deliver',
     'access callback'   => TRUE,
  );
  return $items;
}


function custom_api_test_ajax($nid) {

   $viewName = 'tip';   
   $args = array($nid); 
   $displayId = 'block_1'; 
   $view = views_embed_view($viewName, $displayId, $args);
   //return drupal_json(array('test'=>$res));
   ajax_deliver($view);     
}

I am creating a link as follows:

<?php
    $link = array(
        '#type' => 'link',
        '#title' => t('something'),
        '#href' => 'ajaxgettip/ajax/114', #Hard coded nid at the end of path, just for testing
        '#ajax' => array(
            'wrapper' => 'ajax-response-goes-here',
            'method' => 'replace',
            'effect' => 'fade',
        ),
    );

$output = "<div id='ajax-response-goes-here'>Some HTML and stuff</div>" . drupal_render($link);
print $output;
?>

I don't have an idea about what I did wrong, and I have read a whole lot of documentation. Who can help me?

1
2

Try with page arguments in hook_menu:

function custom_api_menu() {
  $items['ajaxgettip/nojs/%'] = array(
      'page callback' => 'custom_api_test_ajax',
      'page arguments' => array(2),
      'type' => MENU_CALLBACK,
      'access callback' => TRUE,
  );
  $items['ajaxgettip/ajax/%'] = array(
      'delivery callback' => 'ajax_deliver',
      'page arguments' => array(2),
      'access callback' => TRUE,
  );
  return $items;
}
0

According to the docs you are correct

"access callback": A function returning TRUE if the user has access rights to this menu item, and FALSE if not. It can also be a boolean constant instead of a function

UPDATE: clearing the cache did it for me. (Now I've got to figure-out why I didn't think of that before)

0

I don't think you need access callback if you're returning true. It should be a string function name or don't specify it at all. Also check if $nid is being passed to the delivery callback function.

Edit: Sorry for bad answer, according to the comments this answer received, you do need to specify access callback as a function or true.

2
  • 3
    That's not true, you do need to specify an access callback. It will default to user access otherwise, in which case the access arguments must be set. The hook_menu() docs has all the info – Clive Mar 3 '12 at 20:15
  • Correct me when I am wrong, but I thing the option "'access callback' => TRUE" means, that anybody has access. I don't think that the problem occurs because of the fact that the $nid variable is empty: When I access the path 'ajaxgettip/ajax/114' directly (the last part of it is a existing nid), I get the same message as mentioned above: [...]"ajaxCallback":"overlay-ajax"}},"merge":true},{"command":"alert","text":"You are not authorized to access this page."}] – user5950 Mar 4 '12 at 1:18
0
/**
* Implementation of hook_menu().
*/
function custom_api_menu() {
  $items['ajaxgettip/nojs/%'] = array(
    'page callback' => 'custom_api_test_ajax',
    'page argument' => array(1,2),
    'type' => MENU_CALLBACK,
    'access callback'   => TRUE,

      );
  $items['ajaxgettip/ajax/%'] = array(
     'delivery callback' => 'ajax_deliver',
      ) +  $items['ajaxgettip/nojs/%'];
  return $items;
}


function custom_api_test_ajax($ajax, $nid) {

   $ajax_true = $ajax === 'ajax';

   $viewName = 'tip';   
   $args = array($nid); 
   $displayId = 'block_1'; 
   $view = views_embed_view($viewName, $displayId, $args);

if ($ajax_true) {
    $commands = array();

    $commands[] = ajax_command_append('#ajax-response-goes-here', '<div>' . $view. '</div>');

    return array(
      '#type' => 'ajax',
      '#commands' => $commands,
    );
  }
  else {
    // Else show the page when nojs page is accessed instead. 
  }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.