Reputation: 351
Does anyone have experience with ajax in drupal 7? I'm a little stuck.
So, with my module, I output a link and map the path to a callback function with hook_menu()
In the callback function I used ajax_command_replace() and ajax_deliver() to update content.
Well, so far, so good. It all works. But turns out, for complicated reasons, that using links won't work.
So instead I decided to try the jQuery ajax way. So I attach a click event to a div so when it gets clicked something like this runs in a JavaScript file that I load:
jQuery.ajax({
type: 'POST',
url: 'http://path/etc',
});
Then, in my module, I use hook menu to map the path to a callback function that looks like this:
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);
}
This is the exact same code that was sucessfully replacing content when I had the links. But for some reason this doesn't work when I try to invoke the ajax call with jQuery. The callback function gets called, the correct stuff gets loaded in $output, but the page isn't updating.
Does anyone know what is going on here?
Upvotes: 1
Views: 2200
Reputation: 41
probably there is some javascript code inside the $output returned by theme('node', $node_view) , which you need to strip(remove). here's some code from my ajax function returning the node rendered content:
$n = node_load($nid, NULL, FALSE);
$output = drupal_render(node_view($n));
$output = preg_replace('~<script\s+type="text/javascript">.+</script>~is', '', $output);
Upvotes: 0
Reputation: 40106
Are you missing a closing parenthesis?
$c = ajax_command_replace('#content','<div id = "content">' .$output. '</div>';
Should be:
$c = ajax_command_replace('#content','<div id = "content">' .$output. '</div>');
Upvotes: 0