I am trying to receive a node form via ajax in Drupal.
Normally I should have created an ahah triggered button that brings and displays nicely the json data. I did it for testing and it worked very nicely with the php code below.
But what i need is to dynamically create custom ahah triggered links with javascript. And these custom links to act like ahah buttons and call the callback function that brings the node form.
On the server side the callback function which send the json data is this:
function get_lesson_form($nid) {
global $user;
$node = array('uid' => $user->uid, 'name' => $user->name, 'type' => 'lesson');
$form_id = 'lesson_node_form';
// Initialize settings:
$file = drupal_get_path('module', 'node') . '/node.pages.inc';
include_once './' . $file;
$rendered_form = drupal_get_form($form_id,$node);
$javascript = drupal_add_js(NULL, NULL);
drupal_json(array(
'status' => TRUE,
'data' => $rendered_form,
'settings' => call_user_func_array('array_merge_recursive',$javascript['setting']),
)
);
}
I looked a little bit how Drupal's ahah.js deals with retrieving and displaying the json content and i created an ajax call below that simply takes tha json data and appends it.
$.ajax({
url : '/fcrp_form/nid',
success : function(response) {
console.log(response);
var form = Drupal.parseJson(response);
var target_wrapper = $('#event-edit-container');
var new_content = $('<div></div>').html(form);
target_wrapper.empty().append(new_content);
Drupal.attachBehaviors($('#event-edit-container'));
},
error : function(response, error) {
console.log(response);
console.log('Error: '+ error);
},
type: 'POST',
dataType: 'json',
});
The problem is that i always receive a parseerror message and the response object is like a mess. I cannot bring the json data successfully and i don't know how this error is occured.
Notice: This happens when the $data has html code. If it has plain text it returns no errors.
Also when I tested with the normal ahah button (the Drupal way), the jquery version 1.3.2 was used, but in my case I use 1.7.2 version.
Any help would be really appreciated.
Sorry for the late answer. Yes, I used Drupal 6.
In the end, the problem turned out to be the different versions of jQuery.
So I used jQuery.noConflict()
and everything seemed to work fine.