1

有人在drupal 7中有ajax经验吗?我有点卡住了。

因此,使用我的模块,我输出一个链接并使用 hook_menu() 将路径映射到回调函数

在回调函数中,我使用了 ajax_command_replace() 和 ajax_deliver() 来更新内容。

嗯,到目前为止,一切都很好。这一切都有效。但事实证明,由于复杂的原因,使用链接是行不通的。

因此,我决定尝试使用 jQuery ajax 方式。所以我将一个点击事件附加到一个 div 上,所以当它被点击时,像这样的东西会在我加载的 JavaScript 文件中运行:

jQuery.ajax({
    type: 'POST',
    url: 'http://path/etc',
});

然后,在我的模块中,我使用挂钩菜单将路径映射到如下所示的回调函数:

function the_callback($var) {
    // a lot of code that gets the right nid to load. This all works...
    // and eventually I end up here:

    $node = node_load($nid, NULL, false);
    if ($node) {
        $node_view = node_view($node); 
        $output = theme("node",$node_view);

        $commands = array();
        $commands = ajax_command_replace('#content','<div id = "content">' . $output . '</div>';
        $page = array('#type' => 'ajax', '#commands' => $commands);
        ajax_deliver($page);

    }

这与我获得链接时成功替换内容的代码完全相同。但是由于某种原因,当我尝试使用 jQuery 调用 ajax 调用时,这不起作用。回调函数被调用,正确的东西被加载到 $output 中,但页面没有更新。

有谁知道这里发生了什么?

4

2 回答 2

0

可能在 theme('node', $node_view) 返回的 $output 中有一些 javascript 代码,您需要删除(删除)这些代码。这是我的 ajax 函数中返回节点渲染内容的一些代码:

  $n = node_load($nid, NULL, FALSE);
  $output =  drupal_render(node_view($n));
  $output = preg_replace('~<script\s+type="text/javascript">.+</script>~is', '', $output);
于 2014-01-18T18:12:53.107 回答
0

您是否缺少右括号?

$c = ajax_command_replace('#content','<div id = "content">' .$output. '</div>';

应该:

$c = ajax_command_replace('#content','<div id = "content">' .$output. '</div>');
于 2013-06-28T22:41:53.920 回答