我正在尝试通过 Drupal 中的 ajax 接收节点表单。
通常我应该创建一个啊触发按钮,它可以很好地显示和显示 json 数据。我这样做是为了测试,它与下面的 php 代码配合得非常好。
但我需要的是用javascript动态创建自定义ahah触发链接。而这些自定义链接的作用就像啊啊按钮,并调用带来节点表单的回调函数。
在服务器端,发送 json 数据的回调函数是这样的:
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']),
)
);
}
我看了一点 Drupal 的 ahah.js 是如何处理检索和显示 json 内容的,我在下面创建了一个 ajax 调用,它只需要 tha json 数据并附加它。
$.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',
});
问题是我总是收到一条parseerror消息,并且响应对象就像一团糟。我无法成功带入 json 数据,也不知道这个错误是如何发生的。
注意:当 $data 有 html 代码时会发生这种情况。如果它有纯文本,它不会返回任何错误。
此外,当我使用普通的 ahah 按钮(Drupal 方式)进行测试时,使用了 jquery 版本 1.3.2,但在我的情况下,我使用的是 1.7.2 版本。
任何帮助将非常感激。