0

I have an AJAX link and a callback function. The problem is that when I click the link an error is thrown.

I have absolutely no idea where did I make an error, the debug information unfortunately doesn't help.

As I understand the AJAX and Form API, the TSTDiv should be replaced with the TST link therefore two links should be visible on the page, right? The problem is, it not only doesn't work, but even throws an (really unhelpful) error.

The callback function / menu hook work fine (correct output is rendered when you access the URL (of the AJAX request) directly).

Code:

function HOOK_menu() {
  $items = array();

  $items['groups_hierarchy_load_more'] = array(
    'page callback' => '_fmg_agh_hierarchy_load_more',
    'access callback' => 'user_access',
    'access arguments' => array('Can view all groups'),
    'type' => MENU_CALLBACK,
  );
...

  $page_array['l'] = array(
    '#type' => 'link',
    '#title' => 'TEST',
    // Note the /nojs portion of the href - if javascript is enabled,
    // this part will be stripped from the path before it is called.
    '#href' => 'groups_hierarchy_load_more/' . '5' . '/nojs',
    '#ajax' => array(
      'wrapper' => 'TSTDiv',
      'method' => 'replace',
    ),
  );

  $page_array['d'] = array(
    '#type' => 'markup',
    '#prefix' => '<div id="TSTDiv">',
    '#suffix' => '</div>',
  );
...

function _fmg_agh_hierarchy_load_more($group_id, $ajax) {
  $page_array = array();

  $page_array['tst']['data'] = array(
    '#type' => 'link',
    '#title' => 'TST',
    // Note the /nojs portion of the href - if javascript is enabled,
    // this part will be stripped from the path before it is called.
    '#href' => 'groups_hierarchy_load_more/' . 'TST' . '/nojs',
  );

  return $page_array;
}

Error:

An AJAX HTTP error occurred.
HTTP Result Code: 200
Debugging information follows.
Path: /drupal7/?q=groups_hierarchy_load_more/5/ajax
StatusText: OK
ResponseText: 
localhost
@import url("http://localhost/drupal7/modules/system/system.base.css?n3277e");
...
0

While I still don't know where the problem is (it should work this way) I've found a solution.

Instead returning the whole $page_array in callback function and hoping that Drupal will understand it and replace the div specified in '#ajax' => array('wrapper' => 'TSTDiv',... property of the link with it you can simply use ajax commands and 'return' something like this:

$commands[] = ajax_command_replace('#TSTDiv', render($page_array));
$page = array('#type' => 'ajax', '#commands' => $commands);
ajax_deliver($page);

And vuala it works just fine (and looks more professional at the same time!).

Would still appreciate and answer why the original approach didn't work, though.

BTW: I didn't know the AJAX commands before.

| improve this answer | |

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.